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 "SkStrokeRec.h" 17 18 class SkBitmap; 19 class SkClipStack; 20 class SkBaseDevice; 21 class SkBlitter; 22 class SkMatrix; 23 class SkPath; 24 class SkRegion; 25 class SkRasterClip; 26 struct SkDrawProcs; 27 struct SkRect; 28 class SkRRect; 29 30 class SkDraw { 31 public: 32 SkDraw(); 33 34 void drawPaint(const SkPaint&) const; 35 void drawPoints(SkCanvas::PointMode, size_t count, const SkPoint[], 36 const SkPaint&, SkBaseDevice*) const; 37 void drawRect(const SkRect& prePaintRect, const SkPaint&, const SkMatrix* paintMatrix, 38 const SkRect* postPaintRect) const; drawRect(const SkRect & rect,const SkPaint & paint)39 void drawRect(const SkRect& rect, const SkPaint& paint) const { 40 this->drawRect(rect, paint, NULL, NULL); 41 } 42 void drawRRect(const SkRRect&, const SkPaint&) const; 43 /** 44 * To save on mallocs, we allow a flag that tells us that srcPath is 45 * mutable, so that we don't have to make copies of it as we transform it. 46 * 47 * If prePathMatrix is not null, it should logically be applied before any 48 * stroking or other effects. If there are no effects on the paint that 49 * affect the geometry/rasterization, then the pre matrix can just be 50 * pre-concated with the current matrix. 51 */ drawPath(const SkPath & path,const SkPaint & paint,const SkMatrix * prePathMatrix,bool pathIsMutable)52 void drawPath(const SkPath& path, const SkPaint& paint, 53 const SkMatrix* prePathMatrix, bool pathIsMutable) const { 54 this->drawPath(path, paint, prePathMatrix, pathIsMutable, false); 55 } 56 57 void drawPath(const SkPath& path, const SkPaint& paint, 58 SkBlitter* customBlitter = NULL) const { 59 this->drawPath(path, paint, NULL, false, false, customBlitter); 60 } 61 62 /* If dstOrNull is null, computes a dst by mapping the bitmap's bounds through the matrix. */ 63 void drawBitmap(const SkBitmap&, const SkMatrix&, const SkRect* dstOrNull, 64 const SkPaint&) const; 65 void drawSprite(const SkBitmap&, int x, int y, const SkPaint&) const; 66 void drawText(const char text[], size_t byteLength, SkScalar x, 67 SkScalar y, const SkPaint& paint, const SkSurfaceProps*) const; 68 void drawPosText(const char text[], size_t byteLength, 69 const SkScalar pos[], int scalarsPerPosition, 70 const SkPoint& offset, const SkPaint&, const SkSurfaceProps*) const; 71 void drawVertices(SkCanvas::VertexMode mode, int count, 72 const SkPoint vertices[], const SkPoint textures[], 73 const SkColor colors[], SkBlendMode bmode, 74 const uint16_t indices[], int ptCount, 75 const SkPaint& paint) const; 76 77 /** 78 * Overwrite the target with the path's coverage (i.e. its mask). 79 * Will overwrite the entire device, so it need not be zero'd first. 80 * 81 * Only device A8 is supported right now. 82 */ 83 void drawPathCoverage(const SkPath& src, const SkPaint& paint, 84 SkBlitter* customBlitter = NULL) const { 85 this->drawPath(src, paint, NULL, false, true, 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 enum RectType { 99 kHair_RectType, 100 kFill_RectType, 101 kStroke_RectType, 102 kPath_RectType 103 }; 104 105 /** 106 * Based on the paint's style, strokeWidth, and the matrix, classify how 107 * to draw the rect. If no special-case is available, returns 108 * kPath_RectType. 109 * 110 * Iff RectType == kStroke_RectType, then strokeSize is set to the device 111 * width and height of the stroke. 112 */ 113 static RectType ComputeRectType(const SkPaint&, const SkMatrix&, 114 SkPoint* strokeSize); 115 116 static bool ShouldDrawTextAsPaths(const SkPaint&, const SkMatrix&); 117 void drawText_asPaths(const char text[], size_t byteLength, SkScalar x, SkScalar y, 118 const SkPaint&) const; 119 void drawPosText_asPaths(const char text[], size_t byteLength, const SkScalar pos[], 120 int scalarsPerPosition, const SkPoint& offset, 121 const SkPaint&, const SkSurfaceProps*) const; 122 static SkScalar ComputeResScaleForStroking(const SkMatrix& ); 123 private: 124 void drawDevMask(const SkMask& mask, const SkPaint&) const; 125 void drawBitmapAsMask(const SkBitmap&, const SkPaint&) const; 126 127 void drawPath(const SkPath&, const SkPaint&, const SkMatrix* preMatrix, 128 bool pathIsMutable, bool drawCoverage, 129 SkBlitter* customBlitter = NULL) const; 130 131 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; 134 /** 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; 144 145 /** Returns the current setting for using fake gamma and contrast. */ 146 uint32_t SK_WARN_UNUSED_RESULT scalerContextFlags() const; 147 148 public: 149 SkPixmap fDst; 150 const SkMatrix* fMatrix; // required 151 const SkRasterClip* fRC; // required 152 153 #ifdef SK_DEBUG 154 void validate() const; 155 #else validate()156 void validate() const {} 157 #endif 158 }; 159 160 #endif 161