blob: 524f300e422c8d19325347b2289b399eee6dcb77 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#ifndef SkDraw_DEFINED
11#define SkDraw_DEFINED
12
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkCanvas.h"
14#include "SkMask.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkPaint.h"
Florin Malitaab244f02017-05-03 19:16:58 +000016#include "SkPixmap.h"
bsalomon055e1922016-05-06 07:22:58 -070017#include "SkStrokeRec.h"
Mike Reed887cdf12017-04-03 11:11:09 -040018#include "SkVertices.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000019
tomhudson@google.coma9e18242013-04-03 10:18:17 +000020class SkBitmap;
reed@google.com7d7ca792011-02-23 22:39:18 +000021class SkClipStack;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000022class SkBaseDevice;
krajcevski53f09592014-08-06 11:12:14 -070023class SkBlitter;
tomhudson@google.coma9e18242013-04-03 10:18:17 +000024class SkMatrix;
reed@android.com8a1c16f2008-12-17 15:59:43 +000025class SkPath;
26class SkRegion;
reed@google.com045e62d2011-10-24 12:19:46 +000027class SkRasterClip;
reed@android.com8a1c16f2008-12-17 15:59:43 +000028struct SkDrawProcs;
tomhudson@google.coma9e18242013-04-03 10:18:17 +000029struct SkRect;
scroggo@google.coma8e33a92013-11-08 18:02:53 +000030class SkRRect;
reed@android.com8a1c16f2008-12-17 15:59:43 +000031
32class SkDraw {
33public:
reed@android.comf2b98d62010-12-20 18:26:13 +000034 SkDraw();
reed@android.com8a1c16f2008-12-17 15:59:43 +000035
36 void drawPaint(const SkPaint&) const;
37 void drawPoints(SkCanvas::PointMode, size_t count, const SkPoint[],
Mike Reed99330ba2017-02-22 11:01:08 -050038 const SkPaint&, SkBaseDevice*) const;
reed03939122014-12-15 13:42:51 -080039 void drawRect(const SkRect& prePaintRect, const SkPaint&, const SkMatrix* paintMatrix,
40 const SkRect* postPaintRect) const;
41 void drawRect(const SkRect& rect, const SkPaint& paint) const {
Ben Wagnera93a14a2017-08-28 10:34:05 -040042 this->drawRect(rect, paint, nullptr, nullptr);
reed03939122014-12-15 13:42:51 -080043 }
scroggo@google.coma8e33a92013-11-08 18:02:53 +000044 void drawRRect(const SkRRect&, const SkPaint&) const;
reed@google.com7ff8d812011-03-25 15:08:16 +000045 /**
46 * To save on mallocs, we allow a flag that tells us that srcPath is
47 * mutable, so that we don't have to make copies of it as we transform it.
48 *
49 * If prePathMatrix is not null, it should logically be applied before any
50 * stroking or other effects. If there are no effects on the paint that
51 * affect the geometry/rasterization, then the pre matrix can just be
52 * pre-concated with the current matrix.
53 */
reed@google.com126f7f52013-11-07 16:06:53 +000054 void drawPath(const SkPath& path, const SkPaint& paint,
Yuqian Li7b6d64a2017-09-01 14:42:49 -040055 const SkMatrix* prePathMatrix = nullptr, bool pathIsMutable = false) const {
reed@google.com126f7f52013-11-07 16:06:53 +000056 this->drawPath(path, paint, prePathMatrix, pathIsMutable, false);
57 }
58
reed03939122014-12-15 13:42:51 -080059 /* If dstOrNull is null, computes a dst by mapping the bitmap's bounds through the matrix. */
60 void drawBitmap(const SkBitmap&, const SkMatrix&, const SkRect* dstOrNull,
61 const SkPaint&) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +000062 void drawSprite(const SkBitmap&, int x, int y, const SkPaint&) const;
63 void drawText(const char text[], size_t byteLength, SkScalar x,
Mike Reed99330ba2017-02-22 11:01:08 -050064 SkScalar y, const SkPaint& paint, const SkSurfaceProps*) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +000065 void drawPosText(const char text[], size_t byteLength,
fmalita05c4a432014-09-29 06:29:53 -070066 const SkScalar pos[], int scalarsPerPosition,
Mike Reed99330ba2017-02-22 11:01:08 -050067 const SkPoint& offset, const SkPaint&, const SkSurfaceProps*) const;
Mike Reed887cdf12017-04-03 11:11:09 -040068 void drawVertices(SkVertices::VertexMode mode, int count,
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 const SkPoint vertices[], const SkPoint textures[],
Mike Reed7d954ad2016-10-28 15:42:34 -040070 const SkColor colors[], SkBlendMode bmode,
reed@android.com8a1c16f2008-12-17 15:59:43 +000071 const uint16_t indices[], int ptCount,
72 const SkPaint& paint) const;
reed@google.com7d7ca792011-02-23 22:39:18 +000073
reed@google.com126f7f52013-11-07 16:06:53 +000074 /**
75 * Overwrite the target with the path's coverage (i.e. its mask).
76 * Will overwrite the entire device, so it need not be zero'd first.
77 *
78 * Only device A8 is supported right now.
79 */
krajcevski53f09592014-08-06 11:12:14 -070080 void drawPathCoverage(const SkPath& src, const SkPaint& paint,
Ben Wagnera93a14a2017-08-28 10:34:05 -040081 SkBlitter* customBlitter = nullptr) const {
Mike Reed65cfb5e2017-06-07 13:03:36 -040082 bool isHairline = paint.getStyle() == SkPaint::kStroke_Style &&
83 paint.getStrokeWidth() > 0;
Ben Wagnera93a14a2017-08-28 10:34:05 -040084 this->drawPath(src, paint, nullptr, false, !isHairline, customBlitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +000085 }
86
87 /** Helper function that creates a mask from a path and an optional maskfilter.
88 Note however, that the resulting mask will not have been actually filtered,
89 that must be done afterwards (by calling filterMask). The maskfilter is provided
90 solely to assist in computing the mask's bounds (if the mode requests that).
91 */
92 static bool DrawToMask(const SkPath& devPath, const SkIRect* clipBounds,
reed@google.com30711b72012-12-18 19:18:39 +000093 const SkMaskFilter*, const SkMatrix* filterMatrix,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +000094 SkMask* mask, SkMask::CreateMode mode,
bsalomon055e1922016-05-06 07:22:58 -070095 SkStrokeRec::InitStyle style);
reed@android.com8a1c16f2008-12-17 15:59:43 +000096
Florin Malita53f77bd2017-04-28 13:48:37 -040097 void drawDevMask(const SkMask& mask, const SkPaint&) const;
98
reed@google.com62ab7ad2011-04-05 14:08:25 +000099 enum RectType {
100 kHair_RectType,
101 kFill_RectType,
102 kStroke_RectType,
103 kPath_RectType
104 };
105
106 /**
107 * Based on the paint's style, strokeWidth, and the matrix, classify how
108 * to draw the rect. If no special-case is available, returns
109 * kPath_RectType.
110 *
111 * Iff RectType == kStroke_RectType, then strokeSize is set to the device
112 * width and height of the stroke.
113 */
114 static RectType ComputeRectType(const SkPaint&, const SkMatrix&,
115 SkPoint* strokeSize);
116
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +0000117 static bool ShouldDrawTextAsPaths(const SkPaint&, const SkMatrix&);
Mike Reed99330ba2017-02-22 11:01:08 -0500118 void drawText_asPaths(const char text[], size_t byteLength, SkScalar x, SkScalar y,
119 const SkPaint&) const;
120 void drawPosText_asPaths(const char text[], size_t byteLength, const SkScalar pos[],
121 int scalarsPerPosition, const SkPoint& offset,
122 const SkPaint&, const SkSurfaceProps*) const;
caryclark1a7eb262016-01-21 07:07:02 -0800123 static SkScalar ComputeResScaleForStroking(const SkMatrix& );
commit-bot@chromium.org5e009892013-10-14 13:42:12 +0000124private:
commit-bot@chromium.org5e009892013-10-14 13:42:12 +0000125 void drawBitmapAsMask(const SkBitmap&, const SkPaint&) const;
126
reed@google.com126f7f52013-11-07 16:06:53 +0000127 void drawPath(const SkPath&, const SkPaint&, const SkMatrix* preMatrix,
krajcevski53f09592014-08-06 11:12:14 -0700128 bool pathIsMutable, bool drawCoverage,
Ben Wagnera93a14a2017-08-28 10:34:05 -0400129 SkBlitter* customBlitter = nullptr) const;
reed@google.com126f7f52013-11-07 16:06:53 +0000130
reed82595b62016-05-09 17:48:46 -0700131 void drawLine(const SkPoint[2], const SkPaint&) const;
132 void drawDevPath(const SkPath& devPath, const SkPaint& paint, bool drawCoverage,
133 SkBlitter* customBlitter, bool doFill) const;
reed@google.com4bbdeac2013-01-24 21:03:11 +0000134 /**
135 * Return the current clip bounds, in local coordinates, with slop to account
136 * for antialiasing or hairlines (i.e. device-bounds outset by 1, and then
137 * run through the inverse of the matrix).
138 *
139 * If the matrix cannot be inverted, or the current clip is empty, return
140 * false and ignore bounds parameter.
141 */
142 bool SK_WARN_UNUSED_RESULT
143 computeConservativeLocalClipBounds(SkRect* bounds) const;
skia.committer@gmail.com4024f322013-01-25 07:06:46 +0000144
brianosmana1e8f8d2016-04-08 06:47:54 -0700145 /** Returns the current setting for using fake gamma and contrast. */
146 uint32_t SK_WARN_UNUSED_RESULT scalerContextFlags() const;
bungemanf6d1e602016-02-22 13:20:28 -0800147
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148public:
reed41e010c2015-06-09 12:16:53 -0700149 SkPixmap fDst;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150 const SkMatrix* fMatrix; // required
reed@google.com045e62d2011-10-24 12:19:46 +0000151 const SkRasterClip* fRC; // required
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152
153#ifdef SK_DEBUG
reed@android.comf2b98d62010-12-20 18:26:13 +0000154 void validate() const;
155#else
156 void validate() const {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000157#endif
158};
159
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160#endif