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 17 class SkBitmap; 18 class SkClipStack; 19 class SkBaseDevice; 20 class SkBlitter; 21 class SkMatrix; 22 class SkPath; 23 class SkRegion; 24 class SkRasterClip; 25 struct SkDrawProcs; 26 struct SkRect; 27 class SkRRect; 28 29 class SkDraw { 30 public: 31 SkDraw(); 32 SkDraw(const SkDraw& src); 33 34 void drawPaint(const SkPaint&) const; 35 void drawPoints(SkCanvas::PointMode, size_t count, const SkPoint[], 36 const SkPaint&, bool forceUseDevice = false) const; 37 void drawRect(const SkRect&, const SkPaint&) const; 38 void drawRRect(const SkRRect&, const SkPaint&) const; 39 /** 40 * To save on mallocs, we allow a flag that tells us that srcPath is 41 * mutable, so that we don't have to make copies of it as we transform it. 42 * 43 * If prePathMatrix is not null, it should logically be applied before any 44 * stroking or other effects. If there are no effects on the paint that 45 * affect the geometry/rasterization, then the pre matrix can just be 46 * pre-concated with the current matrix. 47 */ 48 void drawPath(const SkPath& path, const SkPaint& paint, 49 const SkMatrix* prePathMatrix, bool pathIsMutable) const { 50 this->drawPath(path, paint, prePathMatrix, pathIsMutable, false); 51 } 52 53 void drawPath(const SkPath& path, const SkPaint& paint, 54 SkBlitter* customBlitter = NULL) const { 55 this->drawPath(path, paint, NULL, false, false, customBlitter); 56 } 57 58 void drawBitmap(const SkBitmap&, const SkMatrix&, const SkPaint&) const; 59 void drawSprite(const SkBitmap&, int x, int y, const SkPaint&) const; 60 void drawText(const char text[], size_t byteLength, SkScalar x, 61 SkScalar y, const SkPaint& paint) const; 62 void drawPosText(const char text[], size_t byteLength, 63 const SkScalar pos[], SkScalar constY, 64 int scalarsPerPosition, const SkPaint& paint) const; 65 void drawTextOnPath(const char text[], size_t byteLength, 66 const SkPath&, const SkMatrix*, const SkPaint&) const; 67 void drawVertices(SkCanvas::VertexMode mode, int count, 68 const SkPoint vertices[], const SkPoint textures[], 69 const SkColor colors[], SkXfermode* xmode, 70 const uint16_t indices[], int ptCount, 71 const SkPaint& paint) const; 72 73 /** 74 * Overwrite the target with the path's coverage (i.e. its mask). 75 * Will overwrite the entire device, so it need not be zero'd first. 76 * 77 * Only device A8 is supported right now. 78 */ 79 void drawPathCoverage(const SkPath& src, const SkPaint& paint, 80 SkBlitter* customBlitter = NULL) const { 81 this->drawPath(src, paint, NULL, false, true, customBlitter); 82 } 83 84 /** Helper function that creates a mask from a path and an optional maskfilter. 85 Note however, that the resulting mask will not have been actually filtered, 86 that must be done afterwards (by calling filterMask). The maskfilter is provided 87 solely to assist in computing the mask's bounds (if the mode requests that). 88 */ 89 static bool DrawToMask(const SkPath& devPath, const SkIRect* clipBounds, 90 const SkMaskFilter*, const SkMatrix* filterMatrix, 91 SkMask* mask, SkMask::CreateMode mode, 92 SkPaint::Style style); 93 94 enum RectType { 95 kHair_RectType, 96 kFill_RectType, 97 kStroke_RectType, 98 kPath_RectType 99 }; 100 101 /** 102 * Based on the paint's style, strokeWidth, and the matrix, classify how 103 * to draw the rect. If no special-case is available, returns 104 * kPath_RectType. 105 * 106 * Iff RectType == kStroke_RectType, then strokeSize is set to the device 107 * width and height of the stroke. 108 */ 109 static RectType ComputeRectType(const SkPaint&, const SkMatrix&, 110 SkPoint* strokeSize); 111 112 static bool ShouldDrawTextAsPaths(const SkPaint&, const SkMatrix&); 113 void drawText_asPaths(const char text[], size_t byteLength, 114 SkScalar x, SkScalar y, const SkPaint&) const; 115 void drawPosText_asPaths(const char text[], size_t byteLength, 116 const SkScalar pos[], SkScalar constY, 117 int scalarsPerPosition, const SkPaint&) const; 118 119 private: 120 void drawDevMask(const SkMask& mask, const SkPaint&) const; 121 void drawBitmapAsMask(const SkBitmap&, const SkPaint&) const; 122 123 void drawPath(const SkPath&, const SkPaint&, const SkMatrix* preMatrix, 124 bool pathIsMutable, bool drawCoverage, 125 SkBlitter* customBlitter = NULL) const; 126 127 /** 128 * Return the current clip bounds, in local coordinates, with slop to account 129 * for antialiasing or hairlines (i.e. device-bounds outset by 1, and then 130 * run through the inverse of the matrix). 131 * 132 * If the matrix cannot be inverted, or the current clip is empty, return 133 * false and ignore bounds parameter. 134 */ 135 bool SK_WARN_UNUSED_RESULT 136 computeConservativeLocalClipBounds(SkRect* bounds) const; 137 138 public: 139 const SkBitmap* fBitmap; // required 140 const SkMatrix* fMatrix; // required 141 const SkRegion* fClip; // DEPRECATED 142 const SkRasterClip* fRC; // required 143 144 const SkClipStack* fClipStack; // optional 145 SkBaseDevice* fDevice; // optional 146 SkDrawProcs* fProcs; // optional 147 148 #ifdef SK_DEBUG 149 void validate() const; 150 #else 151 void validate() const {} 152 #endif 153 }; 154 155 #endif 156