SkDevice.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*
  2. * Copyright 2010 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 SkDevice_DEFINED
  8. #define SkDevice_DEFINED
  9. #include "include/core/SkCanvas.h"
  10. #include "include/core/SkColor.h"
  11. #include "include/core/SkRefCnt.h"
  12. #include "include/core/SkRegion.h"
  13. #include "include/core/SkSurfaceProps.h"
  14. #include "include/private/SkNoncopyable.h"
  15. class SkBitmap;
  16. struct SkDrawShadowRec;
  17. class SkGlyphRun;
  18. class SkGlyphRunList;
  19. class SkImageFilterCache;
  20. struct SkIRect;
  21. class SkMatrix;
  22. class SkRasterHandleAllocator;
  23. class SkSpecialImage;
  24. class SkBaseDevice : public SkRefCnt {
  25. public:
  26. SkBaseDevice(const SkImageInfo&, const SkSurfaceProps&);
  27. /**
  28. * Return ImageInfo for this device. If the canvas is not backed by pixels
  29. * (cpu or gpu), then the info's ColorType will be kUnknown_SkColorType.
  30. */
  31. const SkImageInfo& imageInfo() const { return fInfo; }
  32. /**
  33. * Return SurfaceProps for this device.
  34. */
  35. const SkSurfaceProps& surfaceProps() const {
  36. return fSurfaceProps;
  37. }
  38. /**
  39. * Return the bounds of the device in the coordinate space of the root
  40. * canvas. The root device will have its top-left at 0,0, but other devices
  41. * such as those associated with saveLayer may have a non-zero origin.
  42. */
  43. void getGlobalBounds(SkIRect* bounds) const {
  44. SkASSERT(bounds);
  45. const SkIPoint& origin = this->getOrigin();
  46. bounds->setXYWH(origin.x(), origin.y(), this->width(), this->height());
  47. }
  48. SkIRect getGlobalBounds() const {
  49. SkIRect bounds;
  50. this->getGlobalBounds(&bounds);
  51. return bounds;
  52. }
  53. int width() const {
  54. return this->imageInfo().width();
  55. }
  56. int height() const {
  57. return this->imageInfo().height();
  58. }
  59. bool isOpaque() const {
  60. return this->imageInfo().isOpaque();
  61. }
  62. bool writePixels(const SkPixmap&, int x, int y);
  63. /**
  64. * Try to get write-access to the pixels behind the device. If successful, this returns true
  65. * and fills-out the pixmap parameter. On success it also bumps the genID of the underlying
  66. * bitmap.
  67. *
  68. * On failure, returns false and ignores the pixmap parameter.
  69. */
  70. bool accessPixels(SkPixmap* pmap);
  71. /**
  72. * Try to get read-only-access to the pixels behind the device. If successful, this returns
  73. * true and fills-out the pixmap parameter.
  74. *
  75. * On failure, returns false and ignores the pixmap parameter.
  76. */
  77. bool peekPixels(SkPixmap*);
  78. /**
  79. * Return the device's origin: its offset in device coordinates from
  80. * the default origin in its canvas' matrix/clip
  81. */
  82. const SkIPoint& getOrigin() const { return fOrigin; }
  83. virtual void* getRasterHandle() const { return nullptr; }
  84. void save() { this->onSave(); }
  85. void restore(const SkMatrix& ctm) {
  86. this->onRestore();
  87. this->setGlobalCTM(ctm);
  88. }
  89. void clipRect(const SkRect& rect, SkClipOp op, bool aa) {
  90. this->onClipRect(rect, op, aa);
  91. }
  92. void clipRRect(const SkRRect& rrect, SkClipOp op, bool aa) {
  93. this->onClipRRect(rrect, op, aa);
  94. }
  95. void clipPath(const SkPath& path, SkClipOp op, bool aa) {
  96. this->onClipPath(path, op, aa);
  97. }
  98. void clipRegion(const SkRegion& region, SkClipOp op) {
  99. this->onClipRegion(region, op);
  100. }
  101. void androidFramework_setDeviceClipRestriction(SkIRect* mutableClipRestriction) {
  102. this->onSetDeviceClipRestriction(mutableClipRestriction);
  103. }
  104. bool clipIsWideOpen() const;
  105. const SkMatrix& ctm() const { return fCTM; }
  106. void setCTM(const SkMatrix& ctm) {
  107. fCTM = ctm;
  108. }
  109. void setGlobalCTM(const SkMatrix& ctm);
  110. virtual void validateDevBounds(const SkIRect&) {}
  111. protected:
  112. enum TileUsage {
  113. kPossible_TileUsage, //!< the created device may be drawn tiled
  114. kNever_TileUsage, //!< the created device will never be drawn tiled
  115. };
  116. struct TextFlags {
  117. uint32_t fFlags; // SkPaint::getFlags()
  118. };
  119. virtual void onSave() {}
  120. virtual void onRestore() {}
  121. virtual void onClipRect(const SkRect& rect, SkClipOp, bool aa) {}
  122. virtual void onClipRRect(const SkRRect& rrect, SkClipOp, bool aa) {}
  123. virtual void onClipPath(const SkPath& path, SkClipOp, bool aa) {}
  124. virtual void onClipRegion(const SkRegion& deviceRgn, SkClipOp) {}
  125. virtual void onSetDeviceClipRestriction(SkIRect* mutableClipRestriction) {}
  126. virtual bool onClipIsAA() const = 0;
  127. virtual void onAsRgnClip(SkRegion*) const = 0;
  128. enum ClipType {
  129. kEmpty_ClipType,
  130. kRect_ClipType,
  131. kComplex_ClipType
  132. };
  133. virtual ClipType onGetClipType() const = 0;
  134. /** These are called inside the per-device-layer loop for each draw call.
  135. When these are called, we have already applied any saveLayer operations,
  136. and are handling any looping from the paint.
  137. */
  138. virtual void drawPaint(const SkPaint& paint) = 0;
  139. virtual void drawPoints(SkCanvas::PointMode mode, size_t count,
  140. const SkPoint[], const SkPaint& paint) = 0;
  141. virtual void drawRect(const SkRect& r,
  142. const SkPaint& paint) = 0;
  143. virtual void drawRegion(const SkRegion& r,
  144. const SkPaint& paint);
  145. virtual void drawOval(const SkRect& oval,
  146. const SkPaint& paint) = 0;
  147. /** By the time this is called we know that abs(sweepAngle) is in the range [0, 360). */
  148. virtual void drawArc(const SkRect& oval, SkScalar startAngle,
  149. SkScalar sweepAngle, bool useCenter, const SkPaint& paint);
  150. virtual void drawRRect(const SkRRect& rr,
  151. const SkPaint& paint) = 0;
  152. // Default impl calls drawPath()
  153. virtual void drawDRRect(const SkRRect& outer,
  154. const SkRRect& inner, const SkPaint&);
  155. /**
  156. * If pathIsMutable, then the implementation is allowed to cast path to a
  157. * non-const pointer and modify it in place (as an optimization). Canvas
  158. * may do this to implement helpers such as drawOval, by placing a temp
  159. * path on the stack to hold the representation of the oval.
  160. */
  161. virtual void drawPath(const SkPath& path,
  162. const SkPaint& paint,
  163. bool pathIsMutable = false) = 0;
  164. virtual void drawSprite(const SkBitmap& bitmap,
  165. int x, int y, const SkPaint& paint) = 0;
  166. /**
  167. * The default impl. will create a bitmap-shader from the bitmap,
  168. * and call drawRect with it.
  169. */
  170. virtual void drawBitmapRect(const SkBitmap&,
  171. const SkRect* srcOrNull, const SkRect& dst,
  172. const SkPaint& paint,
  173. SkCanvas::SrcRectConstraint) = 0;
  174. virtual void drawBitmapNine(const SkBitmap&, const SkIRect& center,
  175. const SkRect& dst, const SkPaint&);
  176. virtual void drawBitmapLattice(const SkBitmap&, const SkCanvas::Lattice&,
  177. const SkRect& dst, const SkPaint&);
  178. virtual void drawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
  179. const SkPaint&, SkCanvas::SrcRectConstraint);
  180. virtual void drawImageNine(const SkImage*, const SkIRect& center,
  181. const SkRect& dst, const SkPaint&);
  182. virtual void drawImageLattice(const SkImage*, const SkCanvas::Lattice&,
  183. const SkRect& dst, const SkPaint&);
  184. virtual void drawVertices(const SkVertices*, const SkVertices::Bone bones[], int boneCount,
  185. SkBlendMode, const SkPaint&) = 0;
  186. virtual void drawShadow(const SkPath&, const SkDrawShadowRec&);
  187. virtual void drawGlyphRunList(const SkGlyphRunList& glyphRunList) = 0;
  188. // default implementation calls drawVertices
  189. virtual void drawPatch(const SkPoint cubics[12], const SkColor colors[4],
  190. const SkPoint texCoords[4], SkBlendMode, const SkPaint& paint);
  191. // default implementation calls drawPath
  192. virtual void drawAtlas(const SkImage* atlas, const SkRSXform[], const SkRect[],
  193. const SkColor[], int count, SkBlendMode, const SkPaint&);
  194. virtual void drawAnnotation(const SkRect&, const char[], SkData*) {}
  195. // Default impl always calls drawRect() with a solid-color paint, setting it to anti-aliased
  196. // only when all edge flags are set. If there's a clip region, it draws that using drawPath,
  197. // or uses clipPath().
  198. virtual void drawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4],
  199. SkCanvas::QuadAAFlags aaFlags, SkColor color, SkBlendMode mode);
  200. // Default impl uses drawImageRect per entry, being anti-aliased only when an entry's edge flags
  201. // are all set. If there's a clip region, it will be applied using clipPath().
  202. virtual void drawEdgeAAImageSet(const SkCanvas::ImageSetEntry[], int count,
  203. const SkPoint dstClips[], const SkMatrix preViewMatrices[],
  204. const SkPaint& paint, SkCanvas::SrcRectConstraint);
  205. /** The SkDevice passed will be an SkDevice which was returned by a call to
  206. onCreateDevice on this device with kNeverTile_TileExpectation.
  207. */
  208. virtual void drawDevice(SkBaseDevice*, int x, int y, const SkPaint&) = 0;
  209. void drawGlyphRunRSXform(const SkFont&, const SkGlyphID[], const SkRSXform[], int count,
  210. SkPoint origin, const SkPaint& paint);
  211. virtual void drawDrawable(SkDrawable*, const SkMatrix*, SkCanvas*);
  212. virtual void drawSpecial(SkSpecialImage*, int x, int y, const SkPaint&,
  213. SkImage* clipImage, const SkMatrix& clipMatrix);
  214. virtual sk_sp<SkSpecialImage> makeSpecial(const SkBitmap&);
  215. virtual sk_sp<SkSpecialImage> makeSpecial(const SkImage*);
  216. virtual sk_sp<SkSpecialImage> snapSpecial();
  217. virtual void setImmutable() {}
  218. bool readPixels(const SkPixmap&, int x, int y);
  219. virtual sk_sp<SkSpecialImage> snapBackImage(const SkIRect&); // default returns null
  220. ///////////////////////////////////////////////////////////////////////////
  221. virtual GrContext* context() const { return nullptr; }
  222. virtual sk_sp<SkSurface> makeSurface(const SkImageInfo&, const SkSurfaceProps&);
  223. virtual bool onPeekPixels(SkPixmap*) { return false; }
  224. /**
  225. * The caller is responsible for "pre-clipping" the dst. The impl can assume that the dst
  226. * image at the specified x,y offset will fit within the device's bounds.
  227. *
  228. * This is explicitly asserted in readPixels(), the public way to call this.
  229. */
  230. virtual bool onReadPixels(const SkPixmap&, int x, int y);
  231. /**
  232. * The caller is responsible for "pre-clipping" the src. The impl can assume that the src
  233. * image at the specified x,y offset will fit within the device's bounds.
  234. *
  235. * This is explicitly asserted in writePixelsDirect(), the public way to call this.
  236. */
  237. virtual bool onWritePixels(const SkPixmap&, int x, int y);
  238. virtual bool onAccessPixels(SkPixmap*) { return false; }
  239. struct CreateInfo {
  240. static SkPixelGeometry AdjustGeometry(const SkImageInfo&, TileUsage, SkPixelGeometry,
  241. bool preserveLCDText);
  242. // The constructor may change the pixel geometry based on other parameters.
  243. CreateInfo(const SkImageInfo& info,
  244. TileUsage tileUsage,
  245. SkPixelGeometry geo)
  246. : fInfo(info)
  247. , fTileUsage(tileUsage)
  248. , fPixelGeometry(AdjustGeometry(info, tileUsage, geo, false))
  249. {}
  250. CreateInfo(const SkImageInfo& info,
  251. TileUsage tileUsage,
  252. SkPixelGeometry geo,
  253. bool preserveLCDText,
  254. bool trackCoverage,
  255. SkRasterHandleAllocator* allocator)
  256. : fInfo(info)
  257. , fTileUsage(tileUsage)
  258. , fPixelGeometry(AdjustGeometry(info, tileUsage, geo, preserveLCDText))
  259. , fTrackCoverage(trackCoverage)
  260. , fAllocator(allocator)
  261. {}
  262. const SkImageInfo fInfo;
  263. const TileUsage fTileUsage;
  264. const SkPixelGeometry fPixelGeometry;
  265. const bool fTrackCoverage = false;
  266. SkRasterHandleAllocator* fAllocator = nullptr;
  267. };
  268. /**
  269. * Create a new device based on CreateInfo. If the paint is not null, then it represents a
  270. * preview of how the new device will be composed with its creator device (this).
  271. *
  272. * The subclass may be handed this device in drawDevice(), so it must always return
  273. * a device that it knows how to draw, and that it knows how to identify if it is not of the
  274. * same subclass (since drawDevice is passed a SkBaseDevice*). If the subclass cannot fulfill
  275. * that contract (e.g. PDF cannot support some settings on the paint) it should return NULL,
  276. * and the caller may then decide to explicitly create a bitmapdevice, knowing that later
  277. * it could not call drawDevice with it (but it could call drawSprite or drawBitmap).
  278. */
  279. virtual SkBaseDevice* onCreateDevice(const CreateInfo&, const SkPaint*) {
  280. return nullptr;
  281. }
  282. // A helper function used by derived classes to log the scale factor of a bitmap or image draw.
  283. static void LogDrawScaleFactor(const SkMatrix& view, const SkMatrix& srcToDst, SkFilterQuality);
  284. private:
  285. friend class SkAndroidFrameworkUtils;
  286. friend class SkCanvas;
  287. friend struct DeviceCM; //for setMatrixClip
  288. friend class SkDraw;
  289. friend class SkDrawIter;
  290. friend class SkSurface_Raster;
  291. friend class DeviceTestingAccess;
  292. // Temporarily friend the SkGlyphRunBuilder until drawPosText is gone.
  293. friend class SkGlyphRun;
  294. friend class SkGlyphRunList;
  295. friend class SkGlyphRunBuilder;
  296. // used to change the backend's pixels (and possibly config/rowbytes)
  297. // but cannot change the width/height, so there should be no change to
  298. // any clip information.
  299. // TODO: move to SkBitmapDevice
  300. virtual void replaceBitmapBackendForRasterSurface(const SkBitmap&) {}
  301. virtual bool forceConservativeRasterClip() const { return false; }
  302. /**
  303. * Don't call this!
  304. */
  305. virtual GrRenderTargetContext* accessRenderTargetContext() { return nullptr; }
  306. // just called by SkCanvas when built as a layer
  307. void setOrigin(const SkMatrix& ctm, int x, int y);
  308. /** Causes any deferred drawing to the device to be completed.
  309. */
  310. virtual void flush() {}
  311. virtual SkImageFilterCache* getImageFilterCache() { return nullptr; }
  312. friend class SkNoPixelsDevice;
  313. friend class SkBitmapDevice;
  314. void privateResize(int w, int h) {
  315. *const_cast<SkImageInfo*>(&fInfo) = fInfo.makeWH(w, h);
  316. }
  317. SkIPoint fOrigin;
  318. const SkImageInfo fInfo;
  319. const SkSurfaceProps fSurfaceProps;
  320. SkMatrix fCTM;
  321. typedef SkRefCnt INHERITED;
  322. };
  323. class SkNoPixelsDevice : public SkBaseDevice {
  324. public:
  325. SkNoPixelsDevice(const SkIRect& bounds, const SkSurfaceProps& props,
  326. sk_sp<SkColorSpace> colorSpace = nullptr)
  327. : SkBaseDevice(SkImageInfo::Make(bounds.width(), bounds.height(), kUnknown_SkColorType,
  328. kUnknown_SkAlphaType, std::move(colorSpace)), props)
  329. {
  330. // this fails if we enable this assert: DiscardableImageMapTest.GetDiscardableImagesInRectMaxImage
  331. //SkASSERT(bounds.width() >= 0 && bounds.height() >= 0);
  332. }
  333. void resetForNextPicture(const SkIRect& bounds) {
  334. //SkASSERT(bounds.width() >= 0 && bounds.height() >= 0);
  335. this->privateResize(bounds.width(), bounds.height());
  336. }
  337. protected:
  338. // We don't track the clip at all (for performance), but we have to respond to some queries.
  339. // We pretend to be wide-open. We could pretend to always be empty, but that *seems* worse.
  340. void onSave() override {}
  341. void onRestore() override {}
  342. void onClipRect(const SkRect& rect, SkClipOp, bool aa) override {}
  343. void onClipRRect(const SkRRect& rrect, SkClipOp, bool aa) override {}
  344. void onClipPath(const SkPath& path, SkClipOp, bool aa) override {}
  345. void onClipRegion(const SkRegion& deviceRgn, SkClipOp) override {}
  346. void onSetDeviceClipRestriction(SkIRect* mutableClipRestriction) override {}
  347. bool onClipIsAA() const override { return false; }
  348. void onAsRgnClip(SkRegion* rgn) const override {
  349. rgn->setRect(SkIRect::MakeWH(this->width(), this->height()));
  350. }
  351. ClipType onGetClipType() const override {
  352. return kRect_ClipType;
  353. }
  354. void drawPaint(const SkPaint& paint) override {}
  355. void drawPoints(SkCanvas::PointMode, size_t, const SkPoint[], const SkPaint&) override {}
  356. void drawRect(const SkRect&, const SkPaint&) override {}
  357. void drawOval(const SkRect&, const SkPaint&) override {}
  358. void drawRRect(const SkRRect&, const SkPaint&) override {}
  359. void drawPath(const SkPath&, const SkPaint&, bool) override {}
  360. void drawSprite(const SkBitmap&, int, int, const SkPaint&) override {}
  361. void drawBitmapRect(const SkBitmap&, const SkRect*, const SkRect&, const SkPaint&,
  362. SkCanvas::SrcRectConstraint) override {}
  363. void drawDevice(SkBaseDevice*, int, int, const SkPaint&) override {}
  364. void drawGlyphRunList(const SkGlyphRunList& glyphRunList) override {}
  365. void drawVertices(const SkVertices*, const SkVertices::Bone[], int, SkBlendMode,
  366. const SkPaint&) override {}
  367. private:
  368. typedef SkBaseDevice INHERITED;
  369. };
  370. class SkAutoDeviceCTMRestore : SkNoncopyable {
  371. public:
  372. SkAutoDeviceCTMRestore(SkBaseDevice* device, const SkMatrix& ctm)
  373. : fDevice(device)
  374. , fPrevCTM(device->ctm())
  375. {
  376. fDevice->setCTM(ctm);
  377. }
  378. ~SkAutoDeviceCTMRestore() {
  379. fDevice->setCTM(fPrevCTM);
  380. }
  381. private:
  382. SkBaseDevice* fDevice;
  383. const SkMatrix fPrevCTM;
  384. };
  385. #endif