1 2 /* 3 * Copyright 2006 The Android Open Source Project 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9 10 #ifndef SkDraw_DEFINED 11 #define SkDraw_DEFINED 12 13 #include "SkCanvas.h" 14 #include "SkMask.h" 15 #include "SkPaint.h" 16 #include "SkPixmap.h" 17 #include "SkStrokeRec.h" 18 #include "SkVertices.h" 19 #include "SkScalerContext.h" 20 21 class SkBitmap; 22 class SkClipStack; 23 class SkBaseDevice; 24 class SkBlitter; 25 class SkMatrix; 26 class SkPath; 27 class SkRegion; 28 class SkRasterClip; 29 struct SkDrawProcs; 30 struct SkRect; 31 class SkRRect; 32 33 class SkDraw { 34 public: 35 SkDraw(); 36 37 void drawPaint(const SkPaint&) const; 38 void drawPoints(SkCanvas::PointMode, size_t count, const SkPoint[], 39 const SkPaint&, SkBaseDevice*) const; 40 void drawRect(const SkRect& prePaintRect, const SkPaint&, const SkMatrix* paintMatrix, 41 const SkRect* postPaintRect) const; 42 void drawRect(const SkRect& rect, const SkPaint& paint) const { 43 this->drawRect(rect, paint, nullptr, nullptr); 44 } 45 void drawRRect(const SkRRect&, const SkPaint&) const; 46 /** 47 * To save on mallocs, we allow a flag that tells us that srcPath is 48 * mutable, so that we don't have to make copies of it as we transform it. 49 * 50 * If prePathMatrix is not null, it should logically be applied before any 51 * stroking or other effects. If there are no effects on the paint that 52 * affect the geometry/rasterization, then the pre matrix can just be 53 * pre-concated with the current matrix. 54 */ 55 void drawPath(const SkPath& path, const SkPaint& paint, 56 const SkMatrix* prePathMatrix = nullptr, bool pathIsMutable = false) const { 57 this->drawPath(path, paint, prePathMatrix, pathIsMutable, false); 58 } 59 60 /* If dstOrNull is null, computes a dst by mapping the bitmap's bounds through the matrix. */ 61 void drawBitmap(const SkBitmap&, const SkMatrix&, const SkRect* dstOrNull, 62 const SkPaint&) const; 63 void drawSprite(const SkBitmap&, int x, int y, const SkPaint&) const; 64 void drawText(const char text[], size_t byteLength, SkScalar x, 65 SkScalar y, const SkPaint& paint, const SkSurfaceProps*) const; 66 void drawPosText(const char text[], size_t byteLength, 67 const SkScalar pos[], int scalarsPerPosition, 68 const SkPoint& offset, const SkPaint&, const SkSurfaceProps*) const; 69 void drawVertices(SkVertices::VertexMode mode, int count, 70 const SkPoint vertices[], const SkPoint textures[], 71 const SkColor colors[], SkBlendMode bmode, 72 const uint16_t indices[], int ptCount, 73 const SkPaint& paint) const; 74 75 /** 76 * Overwrite the target with the path's coverage (i.e. its mask). 77 * Will overwrite the entire device, so it need not be zero'd first. 78 * 79 * Only device A8 is supported right now. 80 */ 81 void drawPathCoverage(const SkPath& src, const SkPaint& paint, 82 SkBlitter* customBlitter = nullptr) const { 83 bool isHairline = paint.getStyle() == SkPaint::kStroke_Style && 84 paint.getStrokeWidth() > 0; 85 this->drawPath(src, paint, nullptr, false, !isHairline, customBlitter); 86 } 87 88 /** Helper function that creates a mask from a path and an optional maskfilter. 89 Note however, that the resulting mask will not have been actually filtered, 90 that must be done afterwards (by calling filterMask). The maskfilter is provided 91 solely to assist in computing the mask's bounds (if the mode requests that). 92 */ 93 static bool DrawToMask(const SkPath& devPath, const SkIRect* clipBounds, 94 const SkMaskFilter*, const SkMatrix* filterMatrix, 95 SkMask* mask, SkMask::CreateMode mode, 96 SkStrokeRec::InitStyle style); 97 98 void drawDevMask(const SkMask& mask, const SkPaint&) const; 99 100 enum RectType { 101 kHair_RectType, 102 kFill_RectType, 103 kStroke_RectType, 104 kPath_RectType 105 }; 106 107 /** 108 * Based on the paint's style, strokeWidth, and the matrix, classify how 109 * to draw the rect. If no special-case is available, returns 110 * kPath_RectType. 111 * 112 * Iff RectType == kStroke_RectType, then strokeSize is set to the device 113 * width and height of the stroke. 114 */ 115 static RectType ComputeRectType(const SkPaint&, const SkMatrix&, 116 SkPoint* strokeSize); 117 118 static bool ShouldDrawTextAsPaths(const SkPaint&, const SkMatrix&, SkScalar sizeLimit = 1024); 119 void drawText_asPaths(const char text[], size_t byteLength, SkScalar x, SkScalar y, 120 const SkPaint&) const; 121 void drawPosText_asPaths(const char text[], size_t byteLength, const SkScalar pos[], 122 int scalarsPerPosition, const SkPoint& offset, 123 const SkPaint&, const SkSurfaceProps*) const; 124 static SkScalar ComputeResScaleForStroking(const SkMatrix& ); 125 private: 126 void drawBitmapAsMask(const SkBitmap&, const SkPaint&) const; 127 128 void drawPath(const SkPath&, const SkPaint&, const SkMatrix* preMatrix, 129 bool pathIsMutable, bool drawCoverage, 130 SkBlitter* customBlitter = nullptr) const; 131 132 void drawLine(const SkPoint[2], const SkPaint&) const; 133 void drawDevPath(const SkPath& devPath, const SkPaint& paint, bool drawCoverage, 134 SkBlitter* customBlitter, bool doFill) const; 135 /** 136 * Return the current clip bounds, in local coordinates, with slop to account 137 * for antialiasing or hairlines (i.e. device-bounds outset by 1, and then 138 * run through the inverse of the matrix). 139 * 140 * If the matrix cannot be inverted, or the current clip is empty, return 141 * false and ignore bounds parameter. 142 */ 143 bool SK_WARN_UNUSED_RESULT 144 computeConservativeLocalClipBounds(SkRect* bounds) const; 145 146 /** Returns the current setting for using fake gamma and contrast. */ 147 SkScalerContextFlags SK_WARN_UNUSED_RESULT scalerContextFlags() const; 148 149 public: 150 SkPixmap fDst; 151 const SkMatrix* fMatrix; // required 152 const SkRasterClip* fRC; // required 153 154 #ifdef SK_DEBUG 155 void validate() const; 156 #else 157 void validate() const {} 158 #endif 159 }; 160 161 #endif 162