reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame^] | 1 | /* libs/graphics/sgl/SkDraw.cpp |
| 2 | ** |
| 3 | ** Copyright 2006, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include "SkDraw.h" |
| 19 | #include "SkBlitter.h" |
| 20 | #include "SkBounder.h" |
| 21 | #include "SkCanvas.h" |
| 22 | #include "SkColorPriv.h" |
| 23 | #include "SkDevice.h" |
| 24 | #include "SkMaskFilter.h" |
| 25 | #include "SkPaint.h" |
| 26 | #include "SkPathEffect.h" |
| 27 | #include "SkRasterizer.h" |
| 28 | #include "SkScan.h" |
| 29 | #include "SkShader.h" |
| 30 | #include "SkStroke.h" |
| 31 | #include "SkTemplatesPriv.h" |
| 32 | #include "SkUtils.h" |
| 33 | |
| 34 | #include "SkAutoKern.h" |
| 35 | #include "SkBitmapProcShader.h" |
| 36 | #include "SkDrawProcs.h" |
| 37 | |
| 38 | //#define TRACE_BITMAP_DRAWS |
| 39 | |
| 40 | class SkAutoRestoreBounder : SkNoncopyable { |
| 41 | public: |
| 42 | // note: initializing fBounder is done only to fix a warning |
| 43 | SkAutoRestoreBounder() : fDraw(NULL), fBounder(NULL) {} |
| 44 | ~SkAutoRestoreBounder() { |
| 45 | if (fDraw) { |
| 46 | fDraw->fBounder = fBounder; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | void clearBounder(const SkDraw* draw) { |
| 51 | fDraw = const_cast<SkDraw*>(draw); |
| 52 | fBounder = draw->fBounder; |
| 53 | fDraw->fBounder = NULL; |
| 54 | } |
| 55 | |
| 56 | private: |
| 57 | SkDraw* fDraw; |
| 58 | SkBounder* fBounder; |
| 59 | }; |
| 60 | |
| 61 | static SkPoint* rect_points(SkRect& r, int index) { |
| 62 | SkASSERT((unsigned)index < 2); |
| 63 | return &((SkPoint*)(void*)&r)[index]; |
| 64 | } |
| 65 | |
| 66 | /** Helper for allocating small blitters on the stack. |
| 67 | */ |
| 68 | |
| 69 | #define kBlitterStorageLongCount (sizeof(SkBitmapProcShader) >> 2) |
| 70 | |
| 71 | class SkAutoBlitterChoose { |
| 72 | public: |
| 73 | SkAutoBlitterChoose(const SkBitmap& device, const SkMatrix& matrix, |
| 74 | const SkPaint& paint) { |
| 75 | fBlitter = SkBlitter::Choose(device, matrix, paint, |
| 76 | fStorage, sizeof(fStorage)); |
| 77 | } |
| 78 | ~SkAutoBlitterChoose(); |
| 79 | |
| 80 | SkBlitter* operator->() { return fBlitter; } |
| 81 | SkBlitter* get() const { return fBlitter; } |
| 82 | |
| 83 | private: |
| 84 | SkBlitter* fBlitter; |
| 85 | uint32_t fStorage[kBlitterStorageLongCount]; |
| 86 | }; |
| 87 | |
| 88 | SkAutoBlitterChoose::~SkAutoBlitterChoose() { |
| 89 | if ((void*)fBlitter == (void*)fStorage) { |
| 90 | fBlitter->~SkBlitter(); |
| 91 | } else { |
| 92 | SkDELETE(fBlitter); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | class SkAutoBitmapShaderInstall { |
| 97 | public: |
| 98 | SkAutoBitmapShaderInstall(const SkBitmap& src, const SkPaint* paint) |
| 99 | : fPaint((SkPaint*)paint) { |
| 100 | fPrevShader = paint->getShader(); |
| 101 | fPrevShader->safeRef(); |
| 102 | fPaint->setShader(SkShader::CreateBitmapShader( src, |
| 103 | SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, |
| 104 | fStorage, sizeof(fStorage))); |
| 105 | } |
| 106 | ~SkAutoBitmapShaderInstall() { |
| 107 | SkShader* shader = fPaint->getShader(); |
| 108 | |
| 109 | fPaint->setShader(fPrevShader); |
| 110 | fPrevShader->safeUnref(); |
| 111 | |
| 112 | if ((void*)shader == (void*)fStorage) { |
| 113 | shader->~SkShader(); |
| 114 | } else { |
| 115 | SkDELETE(shader); |
| 116 | } |
| 117 | } |
| 118 | private: |
| 119 | SkPaint* fPaint; |
| 120 | SkShader* fPrevShader; |
| 121 | uint32_t fStorage[kBlitterStorageLongCount]; |
| 122 | }; |
| 123 | |
| 124 | class SkAutoPaintStyleRestore { |
| 125 | public: |
| 126 | SkAutoPaintStyleRestore(const SkPaint& paint, SkPaint::Style style) |
| 127 | : fPaint((SkPaint&)paint) { |
| 128 | fStyle = paint.getStyle(); // record the old |
| 129 | fPaint.setStyle(style); // change it to the specified style |
| 130 | } |
| 131 | ~SkAutoPaintStyleRestore() { |
| 132 | fPaint.setStyle(fStyle); // restore the old |
| 133 | } |
| 134 | private: |
| 135 | SkPaint& fPaint; |
| 136 | SkPaint::Style fStyle; |
| 137 | |
| 138 | // illegal |
| 139 | SkAutoPaintStyleRestore(const SkAutoPaintStyleRestore&); |
| 140 | SkAutoPaintStyleRestore& operator=(const SkAutoPaintStyleRestore&); |
| 141 | }; |
| 142 | |
| 143 | /////////////////////////////////////////////////////////////////////////////// |
| 144 | |
| 145 | SkDraw::SkDraw(const SkDraw& src) { |
| 146 | memcpy(this, &src, sizeof(*this)); |
| 147 | } |
| 148 | |
| 149 | /////////////////////////////////////////////////////////////////////////////// |
| 150 | |
| 151 | typedef void (*BitmapXferProc)(void* pixels, size_t bytes, uint32_t data); |
| 152 | |
| 153 | static void D_Clear_BitmapXferProc(void* pixels, size_t bytes, uint32_t) { |
| 154 | bzero(pixels, bytes); |
| 155 | } |
| 156 | |
| 157 | static void D_Dst_BitmapXferProc(void*, size_t, uint32_t data) {} |
| 158 | |
| 159 | static void D32_Src_BitmapXferProc(void* pixels, size_t bytes, uint32_t data) { |
| 160 | sk_memset32((uint32_t*)pixels, data, bytes >> 2); |
| 161 | } |
| 162 | |
| 163 | static void D16_Src_BitmapXferProc(void* pixels, size_t bytes, uint32_t data) { |
| 164 | sk_memset16((uint16_t*)pixels, data, bytes >> 1); |
| 165 | } |
| 166 | |
| 167 | static void DA8_Src_BitmapXferProc(void* pixels, size_t bytes, uint32_t data) { |
| 168 | memset(pixels, data, bytes); |
| 169 | } |
| 170 | |
| 171 | static BitmapXferProc ChooseBitmapXferProc(const SkBitmap& bitmap, |
| 172 | const SkPaint& paint, |
| 173 | uint32_t* data) { |
| 174 | // todo: we can apply colorfilter up front if no shader, so we wouldn't |
| 175 | // need to abort this fastpath |
| 176 | if (paint.getShader() || paint.getColorFilter()) { |
| 177 | return NULL; |
| 178 | } |
| 179 | |
| 180 | SkPorterDuff::Mode mode; |
| 181 | if (!SkPorterDuff::IsMode(paint.getXfermode(), &mode)) { |
| 182 | return NULL; |
| 183 | } |
| 184 | |
| 185 | SkColor color = paint.getColor(); |
| 186 | |
| 187 | // collaps modes based on color... |
| 188 | if (SkPorterDuff::kSrcOver_Mode == mode) { |
| 189 | unsigned alpha = SkColorGetA(color); |
| 190 | if (0 == alpha) { |
| 191 | mode = SkPorterDuff::kDst_Mode; |
| 192 | } else if (0xFF == alpha) { |
| 193 | mode = SkPorterDuff::kSrc_Mode; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | switch (mode) { |
| 198 | case SkPorterDuff::kClear_Mode: |
| 199 | // SkDebugf("--- D_Clear_BitmapXferProc\n"); |
| 200 | return D_Clear_BitmapXferProc; // ignore data |
| 201 | case SkPorterDuff::kDst_Mode: |
| 202 | // SkDebugf("--- D_Dst_BitmapXferProc\n"); |
| 203 | return D_Dst_BitmapXferProc; // ignore data |
| 204 | case SkPorterDuff::kSrc_Mode: { |
| 205 | /* |
| 206 | should I worry about dithering for the lower depths? |
| 207 | */ |
| 208 | SkPMColor pmc = SkPreMultiplyColor(color); |
| 209 | switch (bitmap.config()) { |
| 210 | case SkBitmap::kARGB_8888_Config: |
| 211 | if (data) { |
| 212 | *data = pmc; |
| 213 | } |
| 214 | // SkDebugf("--- D32_Src_BitmapXferProc\n"); |
| 215 | return D32_Src_BitmapXferProc; |
| 216 | case SkBitmap::kARGB_4444_Config: |
| 217 | if (data) { |
| 218 | *data = SkPixel32ToPixel4444(pmc); |
| 219 | } |
| 220 | // SkDebugf("--- D16_Src_BitmapXferProc\n"); |
| 221 | return D16_Src_BitmapXferProc; |
| 222 | case SkBitmap::kRGB_565_Config: |
| 223 | if (data) { |
| 224 | *data = SkPixel32ToPixel16(pmc); |
| 225 | } |
| 226 | // SkDebugf("--- D16_Src_BitmapXferProc\n"); |
| 227 | return D16_Src_BitmapXferProc; |
| 228 | case SkBitmap::kA8_Config: |
| 229 | if (data) { |
| 230 | *data = SkGetPackedA32(pmc); |
| 231 | } |
| 232 | // SkDebugf("--- DA8_Src_BitmapXferProc\n"); |
| 233 | return DA8_Src_BitmapXferProc; |
| 234 | default: |
| 235 | break; |
| 236 | } |
| 237 | break; |
| 238 | } |
| 239 | default: |
| 240 | break; |
| 241 | } |
| 242 | return NULL; |
| 243 | } |
| 244 | |
| 245 | static void CallBitmapXferProc(const SkBitmap& bitmap, const SkIRect& rect, |
| 246 | BitmapXferProc proc, uint32_t procData) { |
| 247 | int shiftPerPixel; |
| 248 | switch (bitmap.config()) { |
| 249 | case SkBitmap::kARGB_8888_Config: |
| 250 | shiftPerPixel = 2; |
| 251 | break; |
| 252 | case SkBitmap::kARGB_4444_Config: |
| 253 | case SkBitmap::kRGB_565_Config: |
| 254 | shiftPerPixel = 1; |
| 255 | break; |
| 256 | case SkBitmap::kA8_Config: |
| 257 | shiftPerPixel = 0; |
| 258 | break; |
| 259 | default: |
| 260 | SkASSERT(!"Can't use xferproc on this config"); |
| 261 | return; |
| 262 | } |
| 263 | |
| 264 | uint8_t* pixels = (uint8_t*)bitmap.getPixels(); |
| 265 | SkASSERT(pixels); |
| 266 | const size_t rowBytes = bitmap.rowBytes(); |
| 267 | const int widthBytes = rect.width() << shiftPerPixel; |
| 268 | |
| 269 | // skip down to the first scanline and X position |
| 270 | pixels += rect.fTop * rowBytes + (rect.fLeft << shiftPerPixel); |
| 271 | for (int scans = rect.height() - 1; scans >= 0; --scans) { |
| 272 | proc(pixels, widthBytes, procData); |
| 273 | pixels += rowBytes; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | void SkDraw::drawPaint(const SkPaint& paint) const { |
| 278 | SkDEBUGCODE(this->validate();) |
| 279 | |
| 280 | if (fClip->isEmpty()) { |
| 281 | return; |
| 282 | } |
| 283 | |
| 284 | SkIRect devRect; |
| 285 | devRect.set(0, 0, fBitmap->width(), fBitmap->height()); |
| 286 | if (fBounder && !fBounder->doIRect(devRect)) { |
| 287 | return; |
| 288 | } |
| 289 | |
| 290 | /* If we don't have a shader (i.e. we're just a solid color) we may |
| 291 | be faster to operate directly on the device bitmap, rather than invoking |
| 292 | a blitter. Esp. true for xfermodes, which require a colorshader to be |
| 293 | present, which is just redundant work. Since we're drawing everywhere |
| 294 | in the clip, we don't have to worry about antialiasing. |
| 295 | */ |
| 296 | uint32_t procData = 0; // to avoid the warning |
| 297 | BitmapXferProc proc = ChooseBitmapXferProc(*fBitmap, paint, &procData); |
| 298 | if (proc) { |
| 299 | if (D_Dst_BitmapXferProc == proc) { // nothing to do |
| 300 | return; |
| 301 | } |
| 302 | |
| 303 | SkRegion::Iterator iter(*fClip); |
| 304 | while (!iter.done()) { |
| 305 | CallBitmapXferProc(*fBitmap, iter.rect(), proc, procData); |
| 306 | iter.next(); |
| 307 | } |
| 308 | } else { |
| 309 | // normal case: use a blitter |
| 310 | SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, paint); |
| 311 | SkScan::FillIRect(devRect, fClip, blitter.get()); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | /////////////////////////////////////////////////////////////////////////////// |
| 316 | |
| 317 | struct PtProcRec { |
| 318 | SkCanvas::PointMode fMode; |
| 319 | const SkPaint* fPaint; |
| 320 | const SkRegion* fClip; |
| 321 | |
| 322 | // computed values |
| 323 | SkFixed fRadius; |
| 324 | |
| 325 | typedef void (*Proc)(const PtProcRec&, const SkPoint devPts[], int count, |
| 326 | SkBlitter*); |
| 327 | |
| 328 | bool init(SkCanvas::PointMode, const SkPaint&, const SkMatrix* matrix, |
| 329 | const SkRegion* clip); |
| 330 | Proc chooseProc(SkBlitter* blitter); |
| 331 | }; |
| 332 | |
| 333 | static void bw_pt_rect_hair_proc(const PtProcRec& rec, const SkPoint devPts[], |
| 334 | int count, SkBlitter* blitter) { |
| 335 | SkASSERT(rec.fClip->isRect()); |
| 336 | const SkIRect& r = rec.fClip->getBounds(); |
| 337 | |
| 338 | for (int i = 0; i < count; i++) { |
| 339 | int x = SkScalarFloor(devPts[i].fX); |
| 340 | int y = SkScalarFloor(devPts[i].fY); |
| 341 | if (r.contains(x, y)) { |
| 342 | blitter->blitH(x, y, 1); |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | static void bw_pt_rect_16_hair_proc(const PtProcRec& rec, |
| 348 | const SkPoint devPts[], int count, |
| 349 | SkBlitter* blitter) { |
| 350 | SkASSERT(rec.fClip->isRect()); |
| 351 | const SkIRect& r = rec.fClip->getBounds(); |
| 352 | uint32_t value; |
| 353 | const SkBitmap* bitmap = blitter->justAnOpaqueColor(&value); |
| 354 | SkASSERT(bitmap); |
| 355 | |
| 356 | uint16_t* addr = bitmap->getAddr16(0, 0); |
| 357 | int rb = bitmap->rowBytes(); |
| 358 | |
| 359 | for (int i = 0; i < count; i++) { |
| 360 | int x = SkScalarFloor(devPts[i].fX); |
| 361 | int y = SkScalarFloor(devPts[i].fY); |
| 362 | if (r.contains(x, y)) { |
| 363 | // *bitmap->getAddr16(x, y) = SkToU16(value); |
| 364 | ((uint16_t*)((char*)addr + y * rb))[x] = SkToU16(value); |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | static void bw_pt_hair_proc(const PtProcRec& rec, const SkPoint devPts[], |
| 370 | int count, SkBlitter* blitter) { |
| 371 | for (int i = 0; i < count; i++) { |
| 372 | int x = SkScalarFloor(devPts[i].fX); |
| 373 | int y = SkScalarFloor(devPts[i].fY); |
| 374 | if (rec.fClip->contains(x, y)) { |
| 375 | blitter->blitH(x, y, 1); |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | static void bw_line_hair_proc(const PtProcRec& rec, const SkPoint devPts[], |
| 381 | int count, SkBlitter* blitter) { |
| 382 | for (int i = 0; i < count; i += 2) { |
| 383 | SkScan::HairLine(devPts[i], devPts[i+1], rec.fClip, blitter); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | static void bw_poly_hair_proc(const PtProcRec& rec, const SkPoint devPts[], |
| 388 | int count, SkBlitter* blitter) { |
| 389 | for (int i = 0; i < count - 1; i++) { |
| 390 | SkScan::HairLine(devPts[i], devPts[i+1], rec.fClip, blitter); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | // aa versions |
| 395 | |
| 396 | static void aa_line_hair_proc(const PtProcRec& rec, const SkPoint devPts[], |
| 397 | int count, SkBlitter* blitter) { |
| 398 | for (int i = 0; i < count; i += 2) { |
| 399 | SkScan::AntiHairLine(devPts[i], devPts[i+1], rec.fClip, blitter); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | static void aa_poly_hair_proc(const PtProcRec& rec, const SkPoint devPts[], |
| 404 | int count, SkBlitter* blitter) { |
| 405 | for (int i = 0; i < count - 1; i++) { |
| 406 | SkScan::AntiHairLine(devPts[i], devPts[i+1], rec.fClip, blitter); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | // square procs (strokeWidth > 0 but matrix is square-scale (sx == sy) |
| 411 | |
| 412 | static void bw_square_proc(const PtProcRec& rec, const SkPoint devPts[], |
| 413 | int count, SkBlitter* blitter) { |
| 414 | const SkFixed radius = rec.fRadius; |
| 415 | for (int i = 0; i < count; i++) { |
| 416 | SkFixed x = SkScalarToFixed(devPts[i].fX); |
| 417 | SkFixed y = SkScalarToFixed(devPts[i].fY); |
| 418 | |
| 419 | SkXRect r; |
| 420 | r.fLeft = x - radius; |
| 421 | r.fTop = y - radius; |
| 422 | r.fRight = x + radius; |
| 423 | r.fBottom = y + radius; |
| 424 | |
| 425 | SkScan::FillXRect(r, rec.fClip, blitter); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | static void aa_square_proc(const PtProcRec& rec, const SkPoint devPts[], |
| 430 | int count, SkBlitter* blitter) { |
| 431 | const SkFixed radius = rec.fRadius; |
| 432 | for (int i = 0; i < count; i++) { |
| 433 | SkFixed x = SkScalarToFixed(devPts[i].fX); |
| 434 | SkFixed y = SkScalarToFixed(devPts[i].fY); |
| 435 | |
| 436 | SkXRect r; |
| 437 | r.fLeft = x - radius; |
| 438 | r.fTop = y - radius; |
| 439 | r.fRight = x + radius; |
| 440 | r.fBottom = y + radius; |
| 441 | |
| 442 | SkScan::AntiFillXRect(r, rec.fClip, blitter); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | bool PtProcRec::init(SkCanvas::PointMode mode, const SkPaint& paint, |
| 447 | const SkMatrix* matrix, const SkRegion* clip) { |
| 448 | if (paint.getPathEffect()) { |
| 449 | return false; |
| 450 | } |
| 451 | SkScalar width = paint.getStrokeWidth(); |
| 452 | if (0 == width) { |
| 453 | fMode = mode; |
| 454 | fPaint = &paint; |
| 455 | fClip = clip; |
| 456 | fRadius = SK_Fixed1 >> 1; |
| 457 | return true; |
| 458 | } |
| 459 | if (matrix->rectStaysRect() && SkCanvas::kPoints_PointMode == mode) { |
| 460 | SkScalar sx = matrix->get(SkMatrix::kMScaleX); |
| 461 | SkScalar sy = matrix->get(SkMatrix::kMScaleY); |
| 462 | if (SkScalarNearlyZero(sx - sy)) { |
| 463 | if (sx < 0) { |
| 464 | sx = -sx; |
| 465 | } |
| 466 | |
| 467 | fMode = mode; |
| 468 | fPaint = &paint; |
| 469 | fClip = clip; |
| 470 | fRadius = SkScalarToFixed(SkScalarMul(width, sx)) >> 1; |
| 471 | return true; |
| 472 | } |
| 473 | } |
| 474 | return false; |
| 475 | } |
| 476 | |
| 477 | PtProcRec::Proc PtProcRec::chooseProc(SkBlitter* blitter) { |
| 478 | Proc proc; |
| 479 | |
| 480 | // for our arrays |
| 481 | SkASSERT(0 == SkCanvas::kPoints_PointMode); |
| 482 | SkASSERT(1 == SkCanvas::kLines_PointMode); |
| 483 | SkASSERT(2 == SkCanvas::kPolygon_PointMode); |
| 484 | SkASSERT((unsigned)fMode <= (unsigned)SkCanvas::kPolygon_PointMode); |
| 485 | |
| 486 | // first check for hairlines |
| 487 | if (0 == fPaint->getStrokeWidth()) { |
| 488 | if (fPaint->isAntiAlias()) { |
| 489 | static const Proc gAAProcs[] = { |
| 490 | aa_square_proc, aa_line_hair_proc, aa_poly_hair_proc |
| 491 | }; |
| 492 | proc = gAAProcs[fMode]; |
| 493 | } else { |
| 494 | if (SkCanvas::kPoints_PointMode == fMode && fClip->isRect()) { |
| 495 | uint32_t value; |
| 496 | const SkBitmap* bm = blitter->justAnOpaqueColor(&value); |
| 497 | if (bm && bm->config() == SkBitmap::kRGB_565_Config) { |
| 498 | proc = bw_pt_rect_16_hair_proc; |
| 499 | } else { |
| 500 | proc = bw_pt_rect_hair_proc; |
| 501 | } |
| 502 | } else { |
| 503 | static Proc gBWProcs[] = { |
| 504 | bw_pt_hair_proc, bw_line_hair_proc, bw_poly_hair_proc |
| 505 | }; |
| 506 | proc = gBWProcs[fMode]; |
| 507 | } |
| 508 | } |
| 509 | } else { |
| 510 | SkASSERT(SkCanvas::kPoints_PointMode == fMode); |
| 511 | if (fPaint->isAntiAlias()) { |
| 512 | proc = aa_square_proc; |
| 513 | } else { |
| 514 | proc = bw_square_proc; |
| 515 | } |
| 516 | } |
| 517 | return proc; |
| 518 | } |
| 519 | |
| 520 | static bool bounder_points(SkBounder* bounder, SkCanvas::PointMode mode, |
| 521 | size_t count, const SkPoint pts[], |
| 522 | const SkPaint& paint, const SkMatrix& matrix) { |
| 523 | SkIRect ibounds; |
| 524 | SkRect bounds; |
| 525 | SkScalar inset = paint.getStrokeWidth(); |
| 526 | |
| 527 | bounds.set(pts, count); |
| 528 | bounds.inset(-inset, -inset); |
| 529 | matrix.mapRect(&bounds); |
| 530 | |
| 531 | bounds.roundOut(&ibounds); |
| 532 | return bounder->doIRect(ibounds); |
| 533 | } |
| 534 | |
| 535 | // each of these costs 8-bytes of stack space, so don't make it too large |
| 536 | // must be even for lines/polygon to work |
| 537 | #define MAX_DEV_PTS 32 |
| 538 | |
| 539 | void SkDraw::drawPoints(SkCanvas::PointMode mode, size_t count, |
| 540 | const SkPoint pts[], const SkPaint& paint) const { |
| 541 | // if we're in lines mode, force count to be even |
| 542 | if (SkCanvas::kLines_PointMode == mode) { |
| 543 | count &= ~(size_t)1; |
| 544 | } |
| 545 | |
| 546 | if ((long)count <= 0) { |
| 547 | return; |
| 548 | } |
| 549 | |
| 550 | SkAutoRestoreBounder arb; |
| 551 | |
| 552 | if (fBounder) { |
| 553 | if (!bounder_points(fBounder, mode, count, pts, paint, *fMatrix)) { |
| 554 | return; |
| 555 | } |
| 556 | // clear the bounder for the rest of this function, so we don't call it |
| 557 | // again later if we happen to call ourselves for drawRect, drawPath, |
| 558 | // etc. |
| 559 | arb.clearBounder(this); |
| 560 | } |
| 561 | |
| 562 | SkASSERT(pts != NULL); |
| 563 | SkDEBUGCODE(this->validate();) |
| 564 | |
| 565 | // nothing to draw |
| 566 | if (fClip->isEmpty() || |
| 567 | (paint.getAlpha() == 0 && paint.getXfermode() == NULL)) { |
| 568 | return; |
| 569 | } |
| 570 | |
| 571 | PtProcRec rec; |
| 572 | if (rec.init(mode, paint, fMatrix, fClip)) { |
| 573 | SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, paint); |
| 574 | |
| 575 | SkPoint devPts[MAX_DEV_PTS]; |
| 576 | const SkMatrix* matrix = fMatrix; |
| 577 | SkBlitter* bltr = blitter.get(); |
| 578 | PtProcRec::Proc proc = rec.chooseProc(bltr); |
| 579 | // we have to back up subsequent passes if we're in polygon mode |
| 580 | const size_t backup = (SkCanvas::kPolygon_PointMode == mode); |
| 581 | |
| 582 | do { |
| 583 | size_t n = count; |
| 584 | if (n > MAX_DEV_PTS) { |
| 585 | n = MAX_DEV_PTS; |
| 586 | } |
| 587 | matrix->mapPoints(devPts, pts, n); |
| 588 | proc(rec, devPts, n, bltr); |
| 589 | pts += n - backup; |
| 590 | SkASSERT(count >= n); |
| 591 | count -= n; |
| 592 | if (count > 0) { |
| 593 | count += backup; |
| 594 | } |
| 595 | } while (count != 0); |
| 596 | } else { |
| 597 | switch (mode) { |
| 598 | case SkCanvas::kPoints_PointMode: { |
| 599 | // temporarily mark the paint as filling. |
| 600 | SkAutoPaintStyleRestore restore(paint, SkPaint::kFill_Style); |
| 601 | |
| 602 | SkScalar width = paint.getStrokeWidth(); |
| 603 | SkScalar radius = SkScalarHalf(width); |
| 604 | |
| 605 | if (paint.getStrokeCap() == SkPaint::kRound_Cap) { |
| 606 | SkPath path; |
| 607 | SkMatrix preMatrix; |
| 608 | |
| 609 | path.addCircle(0, 0, radius); |
| 610 | for (size_t i = 0; i < count; i++) { |
| 611 | preMatrix.setTranslate(pts[i].fX, pts[i].fY); |
| 612 | // pass true for the last point, since we can modify |
| 613 | // then path then |
| 614 | this->drawPath(path, paint, &preMatrix, (count-1) == i); |
| 615 | } |
| 616 | } else { |
| 617 | SkRect r; |
| 618 | |
| 619 | for (size_t i = 0; i < count; i++) { |
| 620 | r.fLeft = pts[i].fX - radius; |
| 621 | r.fTop = pts[i].fY - radius; |
| 622 | r.fRight = r.fLeft + width; |
| 623 | r.fBottom = r.fTop + width; |
| 624 | this->drawRect(r, paint); |
| 625 | } |
| 626 | } |
| 627 | break; |
| 628 | } |
| 629 | case SkCanvas::kLines_PointMode: |
| 630 | case SkCanvas::kPolygon_PointMode: { |
| 631 | count -= 1; |
| 632 | SkPath path; |
| 633 | SkPaint p(paint); |
| 634 | p.setStyle(SkPaint::kStroke_Style); |
| 635 | size_t inc = (SkCanvas::kLines_PointMode == mode) ? 2 : 1; |
| 636 | for (size_t i = 0; i < count; i += inc) { |
| 637 | path.moveTo(pts[i]); |
| 638 | path.lineTo(pts[i+1]); |
| 639 | this->drawPath(path, p, NULL, true); |
| 640 | path.rewind(); |
| 641 | } |
| 642 | break; |
| 643 | } |
| 644 | } |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | static inline SkPoint* as_lefttop(SkRect* r) { |
| 649 | return (SkPoint*)(void*)r; |
| 650 | } |
| 651 | |
| 652 | static inline SkPoint* as_rightbottom(SkRect* r) { |
| 653 | return ((SkPoint*)(void*)r) + 1; |
| 654 | } |
| 655 | |
| 656 | void SkDraw::drawRect(const SkRect& rect, const SkPaint& paint) const { |
| 657 | SkDEBUGCODE(this->validate();) |
| 658 | |
| 659 | // nothing to draw |
| 660 | if (fClip->isEmpty() || |
| 661 | (paint.getAlpha() == 0 && paint.getXfermode() == NULL)) { |
| 662 | return; |
| 663 | } |
| 664 | |
| 665 | // complex enough to draw as a path |
| 666 | if (paint.getPathEffect() || paint.getMaskFilter() || |
| 667 | paint.getRasterizer() || !fMatrix->rectStaysRect() || |
| 668 | (paint.getStyle() != SkPaint::kFill_Style && |
| 669 | SkScalarHalf(paint.getStrokeWidth()) > 0)) { |
| 670 | SkPath tmp; |
| 671 | tmp.addRect(rect); |
| 672 | tmp.setFillType(SkPath::kWinding_FillType); |
| 673 | this->drawPath(tmp, paint); |
| 674 | return; |
| 675 | } |
| 676 | |
| 677 | const SkMatrix& matrix = *fMatrix; |
| 678 | SkRect devRect; |
| 679 | |
| 680 | // transform rect into devRect |
| 681 | { |
| 682 | matrix.mapXY(rect.fLeft, rect.fTop, rect_points(devRect, 0)); |
| 683 | matrix.mapXY(rect.fRight, rect.fBottom, rect_points(devRect, 1)); |
| 684 | devRect.sort(); |
| 685 | } |
| 686 | |
| 687 | if (fBounder && !fBounder->doRect(devRect, paint)) { |
| 688 | return; |
| 689 | } |
| 690 | |
| 691 | // look for the quick exit, before we build a blitter |
| 692 | { |
| 693 | SkIRect ir; |
| 694 | devRect.roundOut(&ir); |
| 695 | if (fClip->quickReject(ir)) |
| 696 | return; |
| 697 | } |
| 698 | |
| 699 | SkAutoBlitterChoose blitterStorage(*fBitmap, matrix, paint); |
| 700 | SkBlitter* blitter = blitterStorage.get(); |
| 701 | const SkRegion* clip = fClip; |
| 702 | |
| 703 | if (paint.getStyle() == SkPaint::kFill_Style) { |
| 704 | if (paint.isAntiAlias()) { |
| 705 | SkScan::AntiFillRect(devRect, clip, blitter); |
| 706 | } else { |
| 707 | SkScan::FillRect(devRect, clip, blitter); |
| 708 | } |
| 709 | } else { |
| 710 | if (paint.isAntiAlias()) { |
| 711 | SkScan::AntiHairRect(devRect, clip, blitter); |
| 712 | } else { |
| 713 | SkScan::HairRect(devRect, clip, blitter); |
| 714 | } |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | void SkDraw::drawDevMask(const SkMask& srcM, const SkPaint& paint) const { |
| 719 | if (srcM.fBounds.isEmpty()) { |
| 720 | return; |
| 721 | } |
| 722 | |
| 723 | SkMask dstM; |
| 724 | const SkMask* mask = &srcM; |
| 725 | |
| 726 | dstM.fImage = NULL; |
| 727 | SkAutoMaskImage ami(&dstM, false); |
| 728 | |
| 729 | if (paint.getMaskFilter() && |
| 730 | paint.getMaskFilter()->filterMask(&dstM, srcM, *fMatrix, NULL)) { |
| 731 | mask = &dstM; |
| 732 | } |
| 733 | |
| 734 | if (fBounder && !fBounder->doIRect(mask->fBounds)) { |
| 735 | return; |
| 736 | } |
| 737 | |
| 738 | SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, paint); |
| 739 | |
| 740 | blitter->blitMaskRegion(*mask, *fClip); |
| 741 | } |
| 742 | |
| 743 | class SkAutoPaintRestoreColorStrokeWidth { |
| 744 | public: |
| 745 | SkAutoPaintRestoreColorStrokeWidth(const SkPaint& paint) { |
| 746 | fPaint = (SkPaint*)&paint; |
| 747 | fColor = paint.getColor(); |
| 748 | fWidth = paint.getStrokeWidth(); |
| 749 | } |
| 750 | ~SkAutoPaintRestoreColorStrokeWidth() { |
| 751 | fPaint->setColor(fColor); |
| 752 | fPaint->setStrokeWidth(fWidth); |
| 753 | } |
| 754 | |
| 755 | private: |
| 756 | SkPaint* fPaint; |
| 757 | SkColor fColor; |
| 758 | SkScalar fWidth; |
| 759 | }; |
| 760 | |
| 761 | void SkDraw::drawPath(const SkPath& origSrcPath, const SkPaint& paint, |
| 762 | const SkMatrix* prePathMatrix, bool pathIsMutable) const { |
| 763 | SkDEBUGCODE(this->validate();) |
| 764 | |
| 765 | // nothing to draw |
| 766 | if (fClip->isEmpty() || |
| 767 | (paint.getAlpha() == 0 && paint.getXfermode() == NULL)) { |
| 768 | return; |
| 769 | } |
| 770 | |
| 771 | SkPath* pathPtr = (SkPath*)&origSrcPath; |
| 772 | bool doFill = true; |
| 773 | SkPath tmpPath; |
| 774 | SkMatrix tmpMatrix; |
| 775 | const SkMatrix* matrix = fMatrix; |
| 776 | |
| 777 | if (prePathMatrix) { |
| 778 | if (paint.getPathEffect() || paint.getStyle() != SkPaint::kFill_Style || |
| 779 | paint.getRasterizer()) { |
| 780 | SkPath* result = pathPtr; |
| 781 | |
| 782 | if (!pathIsMutable) { |
| 783 | result = &tmpPath; |
| 784 | pathIsMutable = true; |
| 785 | } |
| 786 | pathPtr->transform(*prePathMatrix, result); |
| 787 | pathPtr = result; |
| 788 | } else { |
| 789 | if (!tmpMatrix.setConcat(*matrix, *prePathMatrix)) { |
| 790 | // overflow |
| 791 | return; |
| 792 | } |
| 793 | matrix = &tmpMatrix; |
| 794 | } |
| 795 | } |
| 796 | // at this point we're done with prePathMatrix |
| 797 | SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;) |
| 798 | |
| 799 | /* |
| 800 | If the device thickness < 1.0, then make it a hairline, and |
| 801 | modulate alpha if the thickness is even smaller (e.g. thickness == 0.5 |
| 802 | should modulate the alpha by 1/2) |
| 803 | */ |
| 804 | |
| 805 | SkAutoPaintRestoreColorStrokeWidth aprc(paint); |
| 806 | |
| 807 | if (paint.getStyle() == SkPaint::kStroke_Style && |
| 808 | paint.getXfermode() == NULL && |
| 809 | (matrix->getType() & SkMatrix::kPerspective_Mask) == 0) { |
| 810 | SkScalar width = paint.getStrokeWidth(); |
| 811 | if (width > 0) { |
| 812 | width = matrix->mapRadius(paint.getStrokeWidth()); |
| 813 | if (width < SK_Scalar1) { |
| 814 | int scale = (int)SkScalarMul(width, 256); |
| 815 | int alpha = paint.getAlpha() * scale >> 8; |
| 816 | |
| 817 | // pretend to be a hairline, with a modulated alpha |
| 818 | ((SkPaint*)&paint)->setAlpha(alpha); |
| 819 | ((SkPaint*)&paint)->setStrokeWidth(0); |
| 820 | |
| 821 | // SkDebugf("------ convert to hairline %d\n", scale); |
| 822 | } |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | if (paint.getPathEffect() || paint.getStyle() != SkPaint::kFill_Style) { |
| 827 | doFill = paint.getFillPath(*pathPtr, &tmpPath); |
| 828 | pathPtr = &tmpPath; |
| 829 | } |
| 830 | |
| 831 | if (paint.getRasterizer()) { |
| 832 | SkMask mask; |
| 833 | if (paint.getRasterizer()->rasterize(*pathPtr, *matrix, |
| 834 | &fClip->getBounds(), paint.getMaskFilter(), &mask, |
| 835 | SkMask::kComputeBoundsAndRenderImage_CreateMode)) { |
| 836 | this->drawDevMask(mask, paint); |
| 837 | SkMask::FreeImage(mask.fImage); |
| 838 | } |
| 839 | return; |
| 840 | } |
| 841 | |
| 842 | // avoid possibly allocating a new path in transform if we can |
| 843 | SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath; |
| 844 | |
| 845 | // transform the path into device space |
| 846 | pathPtr->transform(*matrix, devPathPtr); |
| 847 | |
| 848 | SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, paint); |
| 849 | |
| 850 | // how does filterPath() know to fill or hairline the path??? <mrr> |
| 851 | if (paint.getMaskFilter() && |
| 852 | paint.getMaskFilter()->filterPath(*devPathPtr, *fMatrix, *fClip, |
| 853 | fBounder, blitter.get())) { |
| 854 | return; // filterPath() called the blitter, so we're done |
| 855 | } |
| 856 | |
| 857 | if (fBounder && !fBounder->doPath(*devPathPtr, paint, doFill)) { |
| 858 | return; |
| 859 | } |
| 860 | |
| 861 | if (doFill) { |
| 862 | if (paint.isAntiAlias()) { |
| 863 | SkScan::AntiFillPath(*devPathPtr, *fClip, blitter.get()); |
| 864 | } else { |
| 865 | SkScan::FillPath(*devPathPtr, *fClip, blitter.get()); |
| 866 | } |
| 867 | } else { // hairline |
| 868 | if (paint.isAntiAlias()) { |
| 869 | SkScan::AntiHairPath(*devPathPtr, fClip, blitter.get()); |
| 870 | } else { |
| 871 | SkScan::HairPath(*devPathPtr, fClip, blitter.get()); |
| 872 | } |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | static inline bool just_translate(const SkMatrix& m) { |
| 877 | return (m.getType() & ~SkMatrix::kTranslate_Mask) == 0; |
| 878 | } |
| 879 | |
| 880 | void SkDraw::drawBitmapAsMask(const SkBitmap& bitmap, |
| 881 | const SkPaint& paint) const { |
| 882 | SkASSERT(bitmap.getConfig() == SkBitmap::kA8_Config); |
| 883 | |
| 884 | if (just_translate(*fMatrix)) { |
| 885 | int ix = SkScalarRound(fMatrix->getTranslateX()); |
| 886 | int iy = SkScalarRound(fMatrix->getTranslateY()); |
| 887 | |
| 888 | SkMask mask; |
| 889 | mask.fBounds.set(ix, iy, ix + bitmap.width(), iy + bitmap.height()); |
| 890 | mask.fFormat = SkMask::kA8_Format; |
| 891 | mask.fRowBytes = bitmap.rowBytes(); |
| 892 | mask.fImage = bitmap.getAddr8(0, 0); |
| 893 | |
| 894 | this->drawDevMask(mask, paint); |
| 895 | } else { // need to xform the bitmap first |
| 896 | SkRect r; |
| 897 | SkMask mask; |
| 898 | |
| 899 | r.set(0, 0, |
| 900 | SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height())); |
| 901 | fMatrix->mapRect(&r); |
| 902 | r.round(&mask.fBounds); |
| 903 | |
| 904 | // set the mask's bounds to the transformed bitmap-bounds, |
| 905 | // clipped to the actual device |
| 906 | { |
| 907 | SkIRect devBounds; |
| 908 | devBounds.set(0, 0, fBitmap->width(), fBitmap->height()); |
| 909 | // need intersect(l, t, r, b) on irect |
| 910 | if (!mask.fBounds.intersect(devBounds)) { |
| 911 | return; |
| 912 | } |
| 913 | } |
| 914 | mask.fFormat = SkMask::kA8_Format; |
| 915 | mask.fRowBytes = SkAlign4(mask.fBounds.width()); |
| 916 | |
| 917 | // allocate (and clear) our temp buffer to hold the transformed bitmap |
| 918 | size_t size = mask.computeImageSize(); |
| 919 | SkAutoMalloc storage(size); |
| 920 | mask.fImage = (uint8_t*)storage.get(); |
| 921 | memset(mask.fImage, 0, size); |
| 922 | |
| 923 | // now draw our bitmap(src) into mask(dst), transformed by the matrix |
| 924 | { |
| 925 | SkBitmap device; |
| 926 | device.setConfig(SkBitmap::kA8_Config, mask.fBounds.width(), |
| 927 | mask.fBounds.height(), mask.fRowBytes); |
| 928 | device.setPixels(mask.fImage); |
| 929 | |
| 930 | SkCanvas c(device); |
| 931 | // need the unclipped top/left for the translate |
| 932 | c.translate(-SkIntToScalar(mask.fBounds.fLeft), |
| 933 | -SkIntToScalar(mask.fBounds.fTop)); |
| 934 | c.concat(*fMatrix); |
| 935 | c.drawBitmap(bitmap, 0, 0, NULL); |
| 936 | } |
| 937 | this->drawDevMask(mask, paint); |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | static bool clipped_out(const SkMatrix& m, const SkRegion& c, |
| 942 | const SkRect& srcR) { |
| 943 | SkRect dstR; |
| 944 | SkIRect devIR; |
| 945 | |
| 946 | m.mapRect(&dstR, srcR); |
| 947 | dstR.roundOut(&devIR); |
| 948 | return c.quickReject(devIR); |
| 949 | } |
| 950 | |
| 951 | static bool clipped_out(const SkMatrix& matrix, const SkRegion& clip, |
| 952 | int width, int height) { |
| 953 | SkRect r; |
| 954 | r.set(0, 0, SkIntToScalar(width), SkIntToScalar(height)); |
| 955 | return clipped_out(matrix, clip, r); |
| 956 | } |
| 957 | |
| 958 | void SkDraw::drawBitmap(const SkBitmap& bitmap, const SkMatrix& prematrix, |
| 959 | const SkPaint& paint) const { |
| 960 | SkDEBUGCODE(this->validate();) |
| 961 | |
| 962 | // nothing to draw |
| 963 | if (fClip->isEmpty() || |
| 964 | bitmap.width() == 0 || bitmap.height() == 0 || |
| 965 | bitmap.getConfig() == SkBitmap::kNo_Config || |
| 966 | (paint.getAlpha() == 0 && paint.getXfermode() == NULL)) { |
| 967 | return; |
| 968 | } |
| 969 | |
| 970 | // run away on too-big bitmaps for now (exceed 16.16) |
| 971 | if (bitmap.width() > 32767 || bitmap.height() > 32767) { |
| 972 | return; |
| 973 | } |
| 974 | |
| 975 | SkAutoPaintStyleRestore restore(paint, SkPaint::kFill_Style); |
| 976 | |
| 977 | SkMatrix matrix; |
| 978 | if (!matrix.setConcat(*fMatrix, prematrix)) { |
| 979 | return; |
| 980 | } |
| 981 | |
| 982 | // do I need to call the bounder first??? |
| 983 | if (clipped_out(matrix, *fClip, bitmap.width(), bitmap.height())) { |
| 984 | return; |
| 985 | } |
| 986 | |
| 987 | // only lock the pixels if we passed the clip test |
| 988 | SkAutoLockPixels alp(bitmap); |
| 989 | // after the lock, check if we are valid |
| 990 | if (!bitmap.readyToDraw()) { |
| 991 | return; |
| 992 | } |
| 993 | |
| 994 | if (bitmap.getConfig() != SkBitmap::kA8_Config && just_translate(matrix)) { |
| 995 | int ix = SkScalarRound(matrix.getTranslateX()); |
| 996 | int iy = SkScalarRound(matrix.getTranslateY()); |
| 997 | uint32_t storage[kBlitterStorageLongCount]; |
| 998 | SkBlitter* blitter = SkBlitter::ChooseSprite(*fBitmap, paint, bitmap, |
| 999 | ix, iy, storage, sizeof(storage)); |
| 1000 | if (blitter) { |
| 1001 | SkAutoTPlacementDelete<SkBlitter> ad(blitter, storage); |
| 1002 | |
| 1003 | SkIRect ir; |
| 1004 | ir.set(ix, iy, ix + bitmap.width(), iy + bitmap.height()); |
| 1005 | |
| 1006 | if (fBounder && !fBounder->doIRect(ir)) { |
| 1007 | return; |
| 1008 | } |
| 1009 | |
| 1010 | SkRegion::Cliperator iter(*fClip, ir); |
| 1011 | const SkIRect& cr = iter.rect(); |
| 1012 | |
| 1013 | for (; !iter.done(); iter.next()) { |
| 1014 | SkASSERT(!cr.isEmpty()); |
| 1015 | blitter->blitRect(cr.fLeft, cr.fTop, cr.width(), cr.height()); |
| 1016 | } |
| 1017 | return; |
| 1018 | } |
| 1019 | #if 0 |
| 1020 | SkDebugf("---- MISSING sprite case: config=%d [%d %d], device=%d, xfer=%p, alpha=0x%X colorFilter=%p\n", |
| 1021 | bitmap.config(), bitmap.width(), bitmap.height(), fBitmap->config(), |
| 1022 | paint.getXfermode(), paint.getAlpha(), paint.getColorFilter()); |
| 1023 | #endif |
| 1024 | } |
| 1025 | |
| 1026 | // now make a temp draw on the stack, and use it |
| 1027 | // |
| 1028 | SkDraw draw(*this); |
| 1029 | draw.fMatrix = &matrix; |
| 1030 | |
| 1031 | if (bitmap.getConfig() == SkBitmap::kA8_Config) { |
| 1032 | draw.drawBitmapAsMask(bitmap, paint); |
| 1033 | } else { |
| 1034 | SkAutoBitmapShaderInstall install(bitmap, &paint); |
| 1035 | |
| 1036 | SkRect r; |
| 1037 | r.set(0, 0, SkIntToScalar(bitmap.width()), |
| 1038 | SkIntToScalar(bitmap.height())); |
| 1039 | // is this ok if paint has a rasterizer? |
| 1040 | draw.drawRect(r, paint); |
| 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | void SkDraw::drawSprite(const SkBitmap& bitmap, int x, int y, |
| 1045 | const SkPaint& paint) const { |
| 1046 | SkDEBUGCODE(this->validate();) |
| 1047 | |
| 1048 | // nothing to draw |
| 1049 | if (fClip->isEmpty() || |
| 1050 | bitmap.width() == 0 || bitmap.height() == 0 || |
| 1051 | bitmap.getConfig() == SkBitmap::kNo_Config || |
| 1052 | (paint.getAlpha() == 0 && paint.getXfermode() == NULL)) { |
| 1053 | return; |
| 1054 | } |
| 1055 | |
| 1056 | SkIRect bounds; |
| 1057 | bounds.set(x, y, x + bitmap.width(), y + bitmap.height()); |
| 1058 | |
| 1059 | if (fClip->quickReject(bounds)) { |
| 1060 | return; // nothing to draw |
| 1061 | } |
| 1062 | |
| 1063 | SkAutoPaintStyleRestore restore(paint, SkPaint::kFill_Style); |
| 1064 | |
| 1065 | if (NULL == paint.getColorFilter()) { |
| 1066 | uint32_t storage[kBlitterStorageLongCount]; |
| 1067 | SkBlitter* blitter = SkBlitter::ChooseSprite(*fBitmap, paint, bitmap, |
| 1068 | x, y, storage, sizeof(storage)); |
| 1069 | |
| 1070 | if (blitter) { |
| 1071 | SkAutoTPlacementDelete<SkBlitter> ad(blitter, storage); |
| 1072 | |
| 1073 | if (fBounder && !fBounder->doIRect(bounds)) { |
| 1074 | return; |
| 1075 | } |
| 1076 | |
| 1077 | SkRegion::Cliperator iter(*fClip, bounds); |
| 1078 | const SkIRect& cr = iter.rect(); |
| 1079 | |
| 1080 | for (; !iter.done(); iter.next()) { |
| 1081 | SkASSERT(!cr.isEmpty()); |
| 1082 | blitter->blitRect(cr.fLeft, cr.fTop, cr.width(), cr.height()); |
| 1083 | } |
| 1084 | return; |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | SkAutoBitmapShaderInstall install(bitmap, &paint); |
| 1089 | |
| 1090 | SkMatrix matrix; |
| 1091 | SkRect r; |
| 1092 | |
| 1093 | // get a scalar version of our rect |
| 1094 | r.set(bounds); |
| 1095 | |
| 1096 | // tell the shader our offset |
| 1097 | matrix.setTranslate(r.fLeft, r.fTop); |
| 1098 | paint.getShader()->setLocalMatrix(matrix); |
| 1099 | |
| 1100 | SkDraw draw(*this); |
| 1101 | matrix.reset(); |
| 1102 | draw.fMatrix = &matrix; |
| 1103 | // call ourself with a rect |
| 1104 | // is this OK if paint has a rasterizer? |
| 1105 | draw.drawRect(r, paint); |
| 1106 | } |
| 1107 | |
| 1108 | /////////////////////////////////////////////////////////////////////////////// |
| 1109 | |
| 1110 | #include "SkScalerContext.h" |
| 1111 | #include "SkGlyphCache.h" |
| 1112 | #include "SkUtils.h" |
| 1113 | |
| 1114 | static void measure_text(SkGlyphCache* cache, SkDrawCacheProc glyphCacheProc, |
| 1115 | const char text[], size_t byteLength, SkVector* stopVector) { |
| 1116 | SkFixed x = 0, y = 0; |
| 1117 | const char* stop = text + byteLength; |
| 1118 | |
| 1119 | SkAutoKern autokern; |
| 1120 | |
| 1121 | while (text < stop) { |
| 1122 | // don't need x, y here, since all subpixel variants will have the |
| 1123 | // same advance |
| 1124 | const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0); |
| 1125 | |
| 1126 | x += autokern.adjust(glyph) + glyph.fAdvanceX; |
| 1127 | y += glyph.fAdvanceY; |
| 1128 | } |
| 1129 | stopVector->set(SkFixedToScalar(x), SkFixedToScalar(y)); |
| 1130 | |
| 1131 | SkASSERT(text == stop); |
| 1132 | } |
| 1133 | |
| 1134 | void SkDraw::drawText_asPaths(const char text[], size_t byteLength, |
| 1135 | SkScalar x, SkScalar y, |
| 1136 | const SkPaint& paint) const { |
| 1137 | SkDEBUGCODE(this->validate();) |
| 1138 | |
| 1139 | SkTextToPathIter iter(text, byteLength, paint, true, true); |
| 1140 | |
| 1141 | SkMatrix matrix; |
| 1142 | matrix.setScale(iter.getPathScale(), iter.getPathScale()); |
| 1143 | matrix.postTranslate(x, y); |
| 1144 | |
| 1145 | const SkPath* iterPath; |
| 1146 | SkScalar xpos, prevXPos = 0; |
| 1147 | |
| 1148 | while ((iterPath = iter.next(&xpos)) != NULL) { |
| 1149 | matrix.postTranslate(xpos - prevXPos, 0); |
| 1150 | this->drawPath(*iterPath, iter.getPaint(), &matrix, false); |
| 1151 | prevXPos = xpos; |
| 1152 | } |
| 1153 | } |
| 1154 | |
| 1155 | #define kStdStrikeThru_Offset (-SK_Scalar1 * 6 / 21) |
| 1156 | #define kStdUnderline_Offset (SK_Scalar1 / 9) |
| 1157 | #define kStdUnderline_Thickness (SK_Scalar1 / 18) |
| 1158 | |
| 1159 | static void draw_paint_rect(const SkDraw* draw, const SkPaint& paint, |
| 1160 | const SkRect& r, SkScalar textSize) { |
| 1161 | if (paint.getStyle() == SkPaint::kFill_Style) { |
| 1162 | draw->drawRect(r, paint); |
| 1163 | } else { |
| 1164 | SkPaint p(paint); |
| 1165 | p.setStrokeWidth(SkScalarMul(textSize, paint.getStrokeWidth())); |
| 1166 | draw->drawRect(r, p); |
| 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | static void handle_aftertext(const SkDraw* draw, const SkPaint& paint, |
| 1171 | SkScalar width, const SkPoint& start) { |
| 1172 | uint32_t flags = paint.getFlags(); |
| 1173 | |
| 1174 | if (flags & (SkPaint::kUnderlineText_Flag | |
| 1175 | SkPaint::kStrikeThruText_Flag)) { |
| 1176 | SkScalar textSize = paint.getTextSize(); |
| 1177 | SkScalar height = SkScalarMul(textSize, kStdUnderline_Thickness); |
| 1178 | SkRect r; |
| 1179 | |
| 1180 | r.fLeft = start.fX; |
| 1181 | r.fRight = start.fX + width; |
| 1182 | |
| 1183 | if (flags & SkPaint::kUnderlineText_Flag) { |
| 1184 | SkScalar offset = SkScalarMulAdd(textSize, kStdUnderline_Offset, |
| 1185 | start.fY); |
| 1186 | r.fTop = offset; |
| 1187 | r.fBottom = offset + height; |
| 1188 | draw_paint_rect(draw, paint, r, textSize); |
| 1189 | } |
| 1190 | if (flags & SkPaint::kStrikeThruText_Flag) { |
| 1191 | SkScalar offset = SkScalarMulAdd(textSize, kStdStrikeThru_Offset, |
| 1192 | start.fY); |
| 1193 | r.fTop = offset; |
| 1194 | r.fBottom = offset + height; |
| 1195 | draw_paint_rect(draw, paint, r, textSize); |
| 1196 | } |
| 1197 | } |
| 1198 | } |
| 1199 | |
| 1200 | // disable warning : local variable used without having been initialized |
| 1201 | #if defined _WIN32 && _MSC_VER >= 1300 |
| 1202 | #pragma warning ( push ) |
| 1203 | #pragma warning ( disable : 4701 ) |
| 1204 | #endif |
| 1205 | |
| 1206 | ////////////////////////////////////////////////////////////////////////////// |
| 1207 | |
| 1208 | static void D1G_NoBounder_RectClip(const SkDraw1Glyph& state, |
| 1209 | const SkGlyph& glyph, int left, int top) { |
| 1210 | SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0); |
| 1211 | SkASSERT(state.fClip->isRect()); |
| 1212 | SkASSERT(NULL == state.fBounder); |
| 1213 | SkASSERT(state.fClipBounds == state.fClip->getBounds()); |
| 1214 | |
| 1215 | left += glyph.fLeft; |
| 1216 | top += glyph.fTop; |
| 1217 | |
| 1218 | int right = left + glyph.fWidth; |
| 1219 | int bottom = top + glyph.fHeight; |
| 1220 | |
| 1221 | SkMask mask; |
| 1222 | SkIRect storage; |
| 1223 | SkIRect* bounds = &mask.fBounds; |
| 1224 | |
| 1225 | mask.fBounds.set(left, top, right, bottom); |
| 1226 | |
| 1227 | // this extra test is worth it, assuming that most of the time it succeeds |
| 1228 | // since we can avoid writing to storage |
| 1229 | if (!state.fClipBounds.containsNoEmptyCheck(left, top, right, bottom)) { |
| 1230 | if (!storage.intersectNoEmptyCheck(mask.fBounds, state.fClipBounds)) |
| 1231 | return; |
| 1232 | bounds = &storage; |
| 1233 | } |
| 1234 | |
| 1235 | uint8_t* aa = (uint8_t*)glyph.fImage; |
| 1236 | if (NULL == aa) { |
| 1237 | aa = (uint8_t*)state.fCache->findImage(glyph); |
| 1238 | if (NULL == aa) { |
| 1239 | return; // can't rasterize glyph |
| 1240 | } |
| 1241 | } |
| 1242 | |
| 1243 | mask.fRowBytes = glyph.rowBytes(); |
| 1244 | mask.fFormat = glyph.fMaskFormat; |
| 1245 | mask.fImage = aa; |
| 1246 | state.fBlitter->blitMask(mask, *bounds); |
| 1247 | } |
| 1248 | |
| 1249 | static void D1G_NoBounder_RgnClip(const SkDraw1Glyph& state, |
| 1250 | const SkGlyph& glyph, int left, int top) { |
| 1251 | SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0); |
| 1252 | SkASSERT(!state.fClip->isRect()); |
| 1253 | SkASSERT(NULL == state.fBounder); |
| 1254 | |
| 1255 | SkMask mask; |
| 1256 | |
| 1257 | left += glyph.fLeft; |
| 1258 | top += glyph.fTop; |
| 1259 | |
| 1260 | mask.fBounds.set(left, top, left + glyph.fWidth, top + glyph.fHeight); |
| 1261 | SkRegion::Cliperator clipper(*state.fClip, mask.fBounds); |
| 1262 | |
| 1263 | if (!clipper.done()) { |
| 1264 | const SkIRect& cr = clipper.rect(); |
| 1265 | const uint8_t* aa = (const uint8_t*)glyph.fImage; |
| 1266 | if (NULL == aa) { |
| 1267 | aa = (uint8_t*)state.fCache->findImage(glyph); |
| 1268 | if (NULL == aa) { |
| 1269 | return; |
| 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | mask.fRowBytes = glyph.rowBytes(); |
| 1274 | mask.fFormat = glyph.fMaskFormat; |
| 1275 | mask.fImage = (uint8_t*)aa; |
| 1276 | do { |
| 1277 | state.fBlitter->blitMask(mask, cr); |
| 1278 | clipper.next(); |
| 1279 | } while (!clipper.done()); |
| 1280 | } |
| 1281 | } |
| 1282 | |
| 1283 | static void D1G_Bounder(const SkDraw1Glyph& state, |
| 1284 | const SkGlyph& glyph, int left, int top) { |
| 1285 | SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0); |
| 1286 | |
| 1287 | SkMask mask; |
| 1288 | |
| 1289 | left += glyph.fLeft; |
| 1290 | top += glyph.fTop; |
| 1291 | |
| 1292 | mask.fBounds.set(left, top, left + glyph.fWidth, top + glyph.fHeight); |
| 1293 | SkRegion::Cliperator clipper(*state.fClip, mask.fBounds); |
| 1294 | |
| 1295 | if (!clipper.done()) { |
| 1296 | const SkIRect& cr = clipper.rect(); |
| 1297 | const uint8_t* aa = (const uint8_t*)glyph.fImage; |
| 1298 | if (NULL == aa) { |
| 1299 | aa = (uint8_t*)state.fCache->findImage(glyph); |
| 1300 | if (NULL == aa) { |
| 1301 | return; |
| 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | if (state.fBounder->doIRect(cr)) { |
| 1306 | mask.fRowBytes = glyph.rowBytes(); |
| 1307 | mask.fFormat = glyph.fMaskFormat; |
| 1308 | mask.fImage = (uint8_t*)aa; |
| 1309 | do { |
| 1310 | state.fBlitter->blitMask(mask, cr); |
| 1311 | clipper.next(); |
| 1312 | } while (!clipper.done()); |
| 1313 | } |
| 1314 | } |
| 1315 | } |
| 1316 | |
| 1317 | SkDraw1Glyph::Proc SkDraw1Glyph::init(const SkDraw* draw, SkBlitter* blitter, |
| 1318 | SkGlyphCache* cache) { |
| 1319 | fDraw = draw; |
| 1320 | fBounder = draw->fBounder; |
| 1321 | fClip = draw->fClip; |
| 1322 | fClipBounds = fClip->getBounds(); |
| 1323 | fBlitter = blitter; |
| 1324 | fCache = cache; |
| 1325 | |
| 1326 | if (draw->fProcs && draw->fProcs->fD1GProc) { |
| 1327 | return draw->fProcs->fD1GProc; |
| 1328 | } |
| 1329 | |
| 1330 | if (NULL == fBounder) { |
| 1331 | if (fClip->isRect()) { |
| 1332 | return D1G_NoBounder_RectClip; |
| 1333 | } else { |
| 1334 | return D1G_NoBounder_RgnClip; |
| 1335 | } |
| 1336 | } else { |
| 1337 | return D1G_Bounder; |
| 1338 | } |
| 1339 | } |
| 1340 | |
| 1341 | enum RoundBaseline { |
| 1342 | kDont_Round_Baseline, |
| 1343 | kRound_X_Baseline, |
| 1344 | kRound_Y_Baseline |
| 1345 | }; |
| 1346 | |
| 1347 | static RoundBaseline computeRoundBaseline(const SkMatrix& mat) { |
| 1348 | if (mat[1] == 0 && mat[3] == 0) { |
| 1349 | // we're 0 or 180 degrees, round the y coordinate of the baseline |
| 1350 | return kRound_Y_Baseline; |
| 1351 | } else if (mat[0] == 0 && mat[4] == 0) { |
| 1352 | // we're 90 or 270 degrees, round the x coordinate of the baseline |
| 1353 | return kRound_X_Baseline; |
| 1354 | } else { |
| 1355 | return kDont_Round_Baseline; |
| 1356 | } |
| 1357 | } |
| 1358 | |
| 1359 | /////////////////////////////////////////////////////////////////////////////// |
| 1360 | |
| 1361 | void SkDraw::drawText(const char text[], size_t byteLength, |
| 1362 | SkScalar x, SkScalar y, const SkPaint& paint) const { |
| 1363 | SkASSERT(byteLength == 0 || text != NULL); |
| 1364 | |
| 1365 | SkDEBUGCODE(this->validate();) |
| 1366 | |
| 1367 | // nothing to draw |
| 1368 | if (text == NULL || byteLength == 0 || |
| 1369 | fClip->isEmpty() || |
| 1370 | (paint.getAlpha() == 0 && paint.getXfermode() == NULL)) { |
| 1371 | return; |
| 1372 | } |
| 1373 | |
| 1374 | SkScalar underlineWidth = 0; |
| 1375 | SkPoint underlineStart; |
| 1376 | |
| 1377 | underlineStart.set(0, 0); // to avoid warning |
| 1378 | if (paint.getFlags() & (SkPaint::kUnderlineText_Flag | |
| 1379 | SkPaint::kStrikeThruText_Flag)) { |
| 1380 | underlineWidth = paint.measureText(text, byteLength); |
| 1381 | |
| 1382 | SkScalar offsetX = 0; |
| 1383 | if (paint.getTextAlign() == SkPaint::kCenter_Align) { |
| 1384 | offsetX = SkScalarHalf(underlineWidth); |
| 1385 | } else if (paint.getTextAlign() == SkPaint::kRight_Align) { |
| 1386 | offsetX = underlineWidth; |
| 1387 | } |
| 1388 | underlineStart.set(x - offsetX, y); |
| 1389 | } |
| 1390 | |
| 1391 | if (/*paint.isLinearText() ||*/ |
| 1392 | (fMatrix->getType() & SkMatrix::kPerspective_Mask)) { |
| 1393 | this->drawText_asPaths(text, byteLength, x, y, paint); |
| 1394 | handle_aftertext(this, paint, underlineWidth, underlineStart); |
| 1395 | return; |
| 1396 | } |
| 1397 | |
| 1398 | SkDrawCacheProc glyphCacheProc = paint.getDrawCacheProc(); |
| 1399 | |
| 1400 | SkAutoGlyphCache autoCache(paint, fMatrix); |
| 1401 | SkGlyphCache* cache = autoCache.getCache(); |
| 1402 | SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, paint); |
| 1403 | |
| 1404 | // transform our starting point |
| 1405 | { |
| 1406 | SkPoint loc; |
| 1407 | fMatrix->mapXY(x, y, &loc); |
| 1408 | x = loc.fX; |
| 1409 | y = loc.fY; |
| 1410 | } |
| 1411 | |
| 1412 | // need to measure first |
| 1413 | if (paint.getTextAlign() != SkPaint::kLeft_Align) { |
| 1414 | SkVector stop; |
| 1415 | |
| 1416 | measure_text(cache, glyphCacheProc, text, byteLength, &stop); |
| 1417 | |
| 1418 | SkScalar stopX = stop.fX; |
| 1419 | SkScalar stopY = stop.fY; |
| 1420 | |
| 1421 | if (paint.getTextAlign() == SkPaint::kCenter_Align) { |
| 1422 | stopX = SkScalarHalf(stopX); |
| 1423 | stopY = SkScalarHalf(stopY); |
| 1424 | } |
| 1425 | x -= stopX; |
| 1426 | y -= stopY; |
| 1427 | } |
| 1428 | |
| 1429 | SkFixed fx = SkScalarToFixed(x); |
| 1430 | SkFixed fy = SkScalarToFixed(y); |
| 1431 | const char* stop = text + byteLength; |
| 1432 | |
| 1433 | if (paint.isSubpixelText()) { |
| 1434 | RoundBaseline roundBaseline = computeRoundBaseline(*fMatrix); |
| 1435 | if (kRound_Y_Baseline == roundBaseline) { |
| 1436 | fy = (fy + 0x8000) & ~0xFFFF; |
| 1437 | } else if (kRound_X_Baseline == roundBaseline) { |
| 1438 | fx = (fx + 0x8000) & ~0xFFFF; |
| 1439 | } |
| 1440 | } else { |
| 1441 | // apply the bias here, so we don't have to add 1/2 in the loop |
| 1442 | fx += SK_Fixed1/2; |
| 1443 | fy += SK_Fixed1/2; |
| 1444 | } |
| 1445 | |
| 1446 | SkAutoKern autokern; |
| 1447 | SkDraw1Glyph d1g; |
| 1448 | SkDraw1Glyph::Proc proc = d1g.init(this, blitter.get(), cache); |
| 1449 | |
| 1450 | while (text < stop) { |
| 1451 | const SkGlyph& glyph = glyphCacheProc(cache, &text, fx, fy); |
| 1452 | |
| 1453 | fx += autokern.adjust(glyph); |
| 1454 | |
| 1455 | if (glyph.fWidth) { |
| 1456 | proc(d1g, glyph, SkFixedFloor(fx), SkFixedFloor(fy)); |
| 1457 | } |
| 1458 | fx += glyph.fAdvanceX; |
| 1459 | fy += glyph.fAdvanceY; |
| 1460 | } |
| 1461 | |
| 1462 | if (underlineWidth) { |
| 1463 | autoCache.release(); // release this now to free up the RAM |
| 1464 | handle_aftertext(this, paint, underlineWidth, underlineStart); |
| 1465 | } |
| 1466 | } |
| 1467 | |
| 1468 | // last parameter is interpreted as SkFixed [x, y] |
| 1469 | // return the fixed position, which may be rounded or not by the caller |
| 1470 | // e.g. subpixel doesn't round |
| 1471 | typedef void (*AlignProc)(const SkPoint&, const SkGlyph&, SkIPoint*); |
| 1472 | |
| 1473 | static void leftAlignProc(const SkPoint& loc, const SkGlyph& glyph, |
| 1474 | SkIPoint* dst) { |
| 1475 | dst->set(SkScalarToFixed(loc.fX), SkScalarToFixed(loc.fY)); |
| 1476 | } |
| 1477 | |
| 1478 | static void centerAlignProc(const SkPoint& loc, const SkGlyph& glyph, |
| 1479 | SkIPoint* dst) { |
| 1480 | dst->set(SkScalarToFixed(loc.fX) - (glyph.fAdvanceX >> 1), |
| 1481 | SkScalarToFixed(loc.fY) - (glyph.fAdvanceY >> 1)); |
| 1482 | } |
| 1483 | |
| 1484 | static void rightAlignProc(const SkPoint& loc, const SkGlyph& glyph, |
| 1485 | SkIPoint* dst) { |
| 1486 | dst->set(SkScalarToFixed(loc.fX) - glyph.fAdvanceX, |
| 1487 | SkScalarToFixed(loc.fY) - glyph.fAdvanceY); |
| 1488 | } |
| 1489 | |
| 1490 | static AlignProc pick_align_proc(SkPaint::Align align) { |
| 1491 | static const AlignProc gProcs[] = { |
| 1492 | leftAlignProc, centerAlignProc, rightAlignProc |
| 1493 | }; |
| 1494 | |
| 1495 | SkASSERT((unsigned)align < SK_ARRAY_COUNT(gProcs)); |
| 1496 | |
| 1497 | return gProcs[align]; |
| 1498 | } |
| 1499 | |
| 1500 | class TextMapState { |
| 1501 | public: |
| 1502 | mutable SkPoint fLoc; |
| 1503 | |
| 1504 | TextMapState(const SkMatrix& matrix, SkScalar y) |
| 1505 | : fMatrix(matrix), fProc(matrix.getMapXYProc()), fY(y) {} |
| 1506 | |
| 1507 | typedef void (*Proc)(const TextMapState&, const SkScalar pos[]); |
| 1508 | |
| 1509 | Proc pickProc(int scalarsPerPosition); |
| 1510 | |
| 1511 | private: |
| 1512 | const SkMatrix& fMatrix; |
| 1513 | SkMatrix::MapXYProc fProc; |
| 1514 | SkScalar fY; // ignored by MapXYProc |
| 1515 | // these are only used by Only... procs |
| 1516 | SkScalar fScaleX, fTransX, fTransformedY; |
| 1517 | |
| 1518 | static void MapXProc(const TextMapState& state, const SkScalar pos[]) { |
| 1519 | state.fProc(state.fMatrix, *pos, state.fY, &state.fLoc); |
| 1520 | } |
| 1521 | |
| 1522 | static void MapXYProc(const TextMapState& state, const SkScalar pos[]) { |
| 1523 | state.fProc(state.fMatrix, pos[0], pos[1], &state.fLoc); |
| 1524 | } |
| 1525 | |
| 1526 | static void MapOnlyScaleXProc(const TextMapState& state, |
| 1527 | const SkScalar pos[]) { |
| 1528 | state.fLoc.set(SkScalarMul(state.fScaleX, *pos) + state.fTransX, |
| 1529 | state.fTransformedY); |
| 1530 | } |
| 1531 | |
| 1532 | static void MapOnlyTransXProc(const TextMapState& state, |
| 1533 | const SkScalar pos[]) { |
| 1534 | state.fLoc.set(*pos + state.fTransX, state.fTransformedY); |
| 1535 | } |
| 1536 | }; |
| 1537 | |
| 1538 | TextMapState::Proc TextMapState::pickProc(int scalarsPerPosition) { |
| 1539 | SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); |
| 1540 | |
| 1541 | if (1 == scalarsPerPosition) { |
| 1542 | unsigned mtype = fMatrix.getType(); |
| 1543 | if (mtype & (SkMatrix::kAffine_Mask | SkMatrix::kPerspective_Mask)) { |
| 1544 | return MapXProc; |
| 1545 | } else { |
| 1546 | fScaleX = fMatrix.getScaleX(); |
| 1547 | fTransX = fMatrix.getTranslateX(); |
| 1548 | fTransformedY = SkScalarMul(fY, fMatrix.getScaleY()) + |
| 1549 | fMatrix.getTranslateY(); |
| 1550 | return (mtype & SkMatrix::kScale_Mask) ? |
| 1551 | MapOnlyScaleXProc : MapOnlyTransXProc; |
| 1552 | } |
| 1553 | } else { |
| 1554 | return MapXYProc; |
| 1555 | } |
| 1556 | } |
| 1557 | |
| 1558 | ////////////////////////////////////////////////////////////////////////////// |
| 1559 | |
| 1560 | void SkDraw::drawPosText(const char text[], size_t byteLength, |
| 1561 | const SkScalar pos[], SkScalar constY, |
| 1562 | int scalarsPerPosition, const SkPaint& paint) const { |
| 1563 | SkASSERT(byteLength == 0 || text != NULL); |
| 1564 | SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); |
| 1565 | |
| 1566 | SkDEBUGCODE(this->validate();) |
| 1567 | |
| 1568 | // nothing to draw |
| 1569 | if (text == NULL || byteLength == 0 || |
| 1570 | fClip->isEmpty() || |
| 1571 | (paint.getAlpha() == 0 && paint.getXfermode() == NULL)) { |
| 1572 | return; |
| 1573 | } |
| 1574 | |
| 1575 | if (/*paint.isLinearText() ||*/ |
| 1576 | (fMatrix->getType() & SkMatrix::kPerspective_Mask)) { |
| 1577 | // TODO !!!! |
| 1578 | // this->drawText_asPaths(text, byteLength, x, y, paint); |
| 1579 | return; |
| 1580 | } |
| 1581 | |
| 1582 | SkDrawCacheProc glyphCacheProc = paint.getDrawCacheProc(); |
| 1583 | SkAutoGlyphCache autoCache(paint, fMatrix); |
| 1584 | SkGlyphCache* cache = autoCache.getCache(); |
| 1585 | SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, paint); |
| 1586 | |
| 1587 | const char* stop = text + byteLength; |
| 1588 | AlignProc alignProc = pick_align_proc(paint.getTextAlign()); |
| 1589 | SkDraw1Glyph d1g; |
| 1590 | SkDraw1Glyph::Proc proc = d1g.init(this, blitter.get(), cache); |
| 1591 | TextMapState tms(*fMatrix, constY); |
| 1592 | TextMapState::Proc tmsProc = tms.pickProc(scalarsPerPosition); |
| 1593 | |
| 1594 | if (paint.isSubpixelText()) { |
| 1595 | // maybe we should skip the rounding if linearText is set |
| 1596 | RoundBaseline roundBaseline = computeRoundBaseline(*fMatrix); |
| 1597 | |
| 1598 | if (SkPaint::kLeft_Align == paint.getTextAlign()) { |
| 1599 | while (text < stop) { |
| 1600 | tmsProc(tms, pos); |
| 1601 | |
| 1602 | SkFixed fx = SkScalarToFixed(tms.fLoc.fX); |
| 1603 | SkFixed fy = SkScalarToFixed(tms.fLoc.fY); |
| 1604 | |
| 1605 | if (kRound_Y_Baseline == roundBaseline) { |
| 1606 | fy = (fy + 0x8000) & ~0xFFFF; |
| 1607 | } else if (kRound_X_Baseline == roundBaseline) { |
| 1608 | fx = (fx + 0x8000) & ~0xFFFF; |
| 1609 | } |
| 1610 | |
| 1611 | const SkGlyph& glyph = glyphCacheProc(cache, &text, fx, fy); |
| 1612 | |
| 1613 | if (glyph.fWidth) { |
| 1614 | proc(d1g, glyph, SkFixedFloor(fx), SkFixedFloor(fy)); |
| 1615 | } |
| 1616 | pos += scalarsPerPosition; |
| 1617 | } |
| 1618 | } else { |
| 1619 | while (text < stop) { |
| 1620 | const SkGlyph* glyph = &glyphCacheProc(cache, &text, 0, 0); |
| 1621 | |
| 1622 | if (glyph->fWidth) { |
| 1623 | SkDEBUGCODE(SkFixed prevAdvX = glyph->fAdvanceX;) |
| 1624 | SkDEBUGCODE(SkFixed prevAdvY = glyph->fAdvanceY;) |
| 1625 | |
| 1626 | SkFixed fx, fy; |
| 1627 | tmsProc(tms, pos); |
| 1628 | |
| 1629 | { |
| 1630 | SkIPoint fixedLoc; |
| 1631 | alignProc(tms.fLoc, *glyph, &fixedLoc); |
| 1632 | fx = fixedLoc.fX; |
| 1633 | fy = fixedLoc.fY; |
| 1634 | |
| 1635 | if (kRound_Y_Baseline == roundBaseline) { |
| 1636 | fy = (fy + 0x8000) & ~0xFFFF; |
| 1637 | } else if (kRound_X_Baseline == roundBaseline) { |
| 1638 | fx = (fx + 0x8000) & ~0xFFFF; |
| 1639 | } |
| 1640 | } |
| 1641 | |
| 1642 | // have to call again, now that we've been "aligned" |
| 1643 | glyph = &glyphCacheProc(cache, &text, fx, fy); |
| 1644 | // the assumption is that the advance hasn't changed |
| 1645 | SkASSERT(prevAdvX == glyph->fAdvanceX); |
| 1646 | SkASSERT(prevAdvY == glyph->fAdvanceY); |
| 1647 | |
| 1648 | proc(d1g, *glyph, SkFixedFloor(fx), SkFixedFloor(fy)); |
| 1649 | } |
| 1650 | pos += scalarsPerPosition; |
| 1651 | } |
| 1652 | } |
| 1653 | } else { // not subpixel |
| 1654 | while (text < stop) { |
| 1655 | // the last 2 parameters are ignored |
| 1656 | const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0); |
| 1657 | |
| 1658 | if (glyph.fWidth) { |
| 1659 | tmsProc(tms, pos); |
| 1660 | |
| 1661 | SkIPoint fixedLoc; |
| 1662 | alignProc(tms.fLoc, glyph, &fixedLoc); |
| 1663 | |
| 1664 | proc(d1g, glyph, |
| 1665 | SkFixedRound(fixedLoc.fX), SkFixedRound(fixedLoc.fY)); |
| 1666 | } |
| 1667 | pos += scalarsPerPosition; |
| 1668 | } |
| 1669 | } |
| 1670 | } |
| 1671 | |
| 1672 | #if defined _WIN32 && _MSC_VER >= 1300 |
| 1673 | #pragma warning ( pop ) |
| 1674 | #endif |
| 1675 | |
| 1676 | /////////////////////////////////////////////////////////////////////////////// |
| 1677 | |
| 1678 | #include "SkPathMeasure.h" |
| 1679 | |
| 1680 | static void morphpoints(SkPoint dst[], const SkPoint src[], int count, |
| 1681 | SkPathMeasure& meas, const SkMatrix& matrix) { |
| 1682 | SkMatrix::MapXYProc proc = matrix.getMapXYProc(); |
| 1683 | |
| 1684 | for (int i = 0; i < count; i++) { |
| 1685 | SkPoint pos; |
| 1686 | SkVector tangent; |
| 1687 | |
| 1688 | proc(matrix, src[i].fX, src[i].fY, &pos); |
| 1689 | SkScalar sx = pos.fX; |
| 1690 | SkScalar sy = pos.fY; |
| 1691 | |
| 1692 | meas.getPosTan(sx, &pos, &tangent); |
| 1693 | |
| 1694 | /* This is the old way (that explains our approach but is way too slow |
| 1695 | SkMatrix matrix; |
| 1696 | SkPoint pt; |
| 1697 | |
| 1698 | pt.set(sx, sy); |
| 1699 | matrix.setSinCos(tangent.fY, tangent.fX); |
| 1700 | matrix.preTranslate(-sx, 0); |
| 1701 | matrix.postTranslate(pos.fX, pos.fY); |
| 1702 | matrix.mapPoints(&dst[i], &pt, 1); |
| 1703 | */ |
| 1704 | dst[i].set(pos.fX - SkScalarMul(tangent.fY, sy), |
| 1705 | pos.fY + SkScalarMul(tangent.fX, sy)); |
| 1706 | } |
| 1707 | } |
| 1708 | |
| 1709 | /* TODO |
| 1710 | |
| 1711 | Need differentially more subdivisions when the follow-path is curvy. Not sure how to |
| 1712 | determine that, but we need it. I guess a cheap answer is let the caller tell us, |
| 1713 | but that seems like a cop-out. Another answer is to get Rob Johnson to figure it out. |
| 1714 | */ |
| 1715 | static void morphpath(SkPath* dst, const SkPath& src, SkPathMeasure& meas, |
| 1716 | const SkMatrix& matrix) { |
| 1717 | SkPath::Iter iter(src, false); |
| 1718 | SkPoint srcP[4], dstP[3]; |
| 1719 | SkPath::Verb verb; |
| 1720 | |
| 1721 | while ((verb = iter.next(srcP)) != SkPath::kDone_Verb) { |
| 1722 | switch (verb) { |
| 1723 | case SkPath::kMove_Verb: |
| 1724 | morphpoints(dstP, srcP, 1, meas, matrix); |
| 1725 | dst->moveTo(dstP[0]); |
| 1726 | break; |
| 1727 | case SkPath::kLine_Verb: |
| 1728 | // turn lines into quads to look bendy |
| 1729 | srcP[0].fX = SkScalarAve(srcP[0].fX, srcP[1].fX); |
| 1730 | srcP[0].fY = SkScalarAve(srcP[0].fY, srcP[1].fY); |
| 1731 | morphpoints(dstP, srcP, 2, meas, matrix); |
| 1732 | dst->quadTo(dstP[0], dstP[1]); |
| 1733 | break; |
| 1734 | case SkPath::kQuad_Verb: |
| 1735 | morphpoints(dstP, &srcP[1], 2, meas, matrix); |
| 1736 | dst->quadTo(dstP[0], dstP[1]); |
| 1737 | break; |
| 1738 | case SkPath::kCubic_Verb: |
| 1739 | morphpoints(dstP, &srcP[1], 3, meas, matrix); |
| 1740 | dst->cubicTo(dstP[0], dstP[1], dstP[2]); |
| 1741 | break; |
| 1742 | case SkPath::kClose_Verb: |
| 1743 | dst->close(); |
| 1744 | break; |
| 1745 | default: |
| 1746 | SkASSERT(!"unknown verb"); |
| 1747 | break; |
| 1748 | } |
| 1749 | } |
| 1750 | } |
| 1751 | |
| 1752 | void SkDraw::drawTextOnPath(const char text[], size_t byteLength, |
| 1753 | const SkPath& follow, const SkMatrix* matrix, |
| 1754 | const SkPaint& paint) const { |
| 1755 | SkASSERT(byteLength == 0 || text != NULL); |
| 1756 | |
| 1757 | // nothing to draw |
| 1758 | if (text == NULL || byteLength == 0 || |
| 1759 | fClip->isEmpty() || |
| 1760 | (paint.getAlpha() == 0 && paint.getXfermode() == NULL)) { |
| 1761 | return; |
| 1762 | } |
| 1763 | |
| 1764 | SkTextToPathIter iter(text, byteLength, paint, true, true); |
| 1765 | SkPathMeasure meas(follow, false); |
| 1766 | SkScalar hOffset = 0; |
| 1767 | |
| 1768 | // need to measure first |
| 1769 | if (paint.getTextAlign() != SkPaint::kLeft_Align) { |
| 1770 | SkScalar pathLen = meas.getLength(); |
| 1771 | if (paint.getTextAlign() == SkPaint::kCenter_Align) { |
| 1772 | pathLen = SkScalarHalf(pathLen); |
| 1773 | } |
| 1774 | hOffset += pathLen; |
| 1775 | } |
| 1776 | |
| 1777 | const SkPath* iterPath; |
| 1778 | SkScalar xpos; |
| 1779 | SkMatrix scaledMatrix; |
| 1780 | SkScalar scale = iter.getPathScale(); |
| 1781 | |
| 1782 | scaledMatrix.setScale(scale, scale); |
| 1783 | |
| 1784 | while ((iterPath = iter.next(&xpos)) != NULL) { |
| 1785 | SkPath tmp; |
| 1786 | SkMatrix m(scaledMatrix); |
| 1787 | |
| 1788 | m.postTranslate(xpos + hOffset, 0); |
| 1789 | if (matrix) { |
| 1790 | m.postConcat(*matrix); |
| 1791 | } |
| 1792 | morphpath(&tmp, *iterPath, meas, m); |
| 1793 | this->drawPath(tmp, iter.getPaint()); |
| 1794 | } |
| 1795 | } |
| 1796 | |
| 1797 | /////////////////////////////////////////////////////////////////////////////// |
| 1798 | |
| 1799 | struct VertState { |
| 1800 | int f0, f1, f2; |
| 1801 | |
| 1802 | VertState(int vCount, const uint16_t indices[], int indexCount) |
| 1803 | : fIndices(indices) { |
| 1804 | fCurrIndex = 0; |
| 1805 | if (indices) { |
| 1806 | fCount = indexCount; |
| 1807 | } else { |
| 1808 | fCount = vCount; |
| 1809 | } |
| 1810 | } |
| 1811 | |
| 1812 | typedef bool (*Proc)(VertState*); |
| 1813 | Proc chooseProc(SkCanvas::VertexMode mode); |
| 1814 | |
| 1815 | private: |
| 1816 | int fCount; |
| 1817 | int fCurrIndex; |
| 1818 | const uint16_t* fIndices; |
| 1819 | |
| 1820 | static bool Triangles(VertState*); |
| 1821 | static bool TrianglesX(VertState*); |
| 1822 | static bool TriangleStrip(VertState*); |
| 1823 | static bool TriangleStripX(VertState*); |
| 1824 | static bool TriangleFan(VertState*); |
| 1825 | static bool TriangleFanX(VertState*); |
| 1826 | }; |
| 1827 | |
| 1828 | bool VertState::Triangles(VertState* state) { |
| 1829 | int index = state->fCurrIndex; |
| 1830 | if (index + 3 > state->fCount) { |
| 1831 | return false; |
| 1832 | } |
| 1833 | state->f0 = index + 0; |
| 1834 | state->f1 = index + 1; |
| 1835 | state->f2 = index + 2; |
| 1836 | state->fCurrIndex = index + 3; |
| 1837 | return true; |
| 1838 | } |
| 1839 | |
| 1840 | bool VertState::TrianglesX(VertState* state) { |
| 1841 | const uint16_t* indices = state->fIndices; |
| 1842 | int index = state->fCurrIndex; |
| 1843 | if (index + 3 > state->fCount) { |
| 1844 | return false; |
| 1845 | } |
| 1846 | state->f0 = indices[index + 0]; |
| 1847 | state->f1 = indices[index + 1]; |
| 1848 | state->f2 = indices[index + 2]; |
| 1849 | state->fCurrIndex = index + 3; |
| 1850 | return true; |
| 1851 | } |
| 1852 | |
| 1853 | bool VertState::TriangleStrip(VertState* state) { |
| 1854 | int index = state->fCurrIndex; |
| 1855 | if (index + 3 > state->fCount) { |
| 1856 | return false; |
| 1857 | } |
| 1858 | state->f2 = index + 2; |
| 1859 | if (index & 1) { |
| 1860 | state->f0 = index + 1; |
| 1861 | state->f1 = index + 0; |
| 1862 | } else { |
| 1863 | state->f0 = index + 0; |
| 1864 | state->f1 = index + 1; |
| 1865 | } |
| 1866 | state->fCurrIndex = index + 1; |
| 1867 | return true; |
| 1868 | } |
| 1869 | |
| 1870 | bool VertState::TriangleStripX(VertState* state) { |
| 1871 | const uint16_t* indices = state->fIndices; |
| 1872 | int index = state->fCurrIndex; |
| 1873 | if (index + 3 > state->fCount) { |
| 1874 | return false; |
| 1875 | } |
| 1876 | state->f2 = indices[index + 2]; |
| 1877 | if (index & 1) { |
| 1878 | state->f0 = indices[index + 1]; |
| 1879 | state->f1 = indices[index + 0]; |
| 1880 | } else { |
| 1881 | state->f0 = indices[index + 0]; |
| 1882 | state->f1 = indices[index + 1]; |
| 1883 | } |
| 1884 | state->fCurrIndex = index + 1; |
| 1885 | return true; |
| 1886 | } |
| 1887 | |
| 1888 | bool VertState::TriangleFan(VertState* state) { |
| 1889 | int index = state->fCurrIndex; |
| 1890 | if (index + 3 > state->fCount) { |
| 1891 | return false; |
| 1892 | } |
| 1893 | state->f0 = 0; |
| 1894 | state->f1 = index + 1; |
| 1895 | state->f2 = index + 2; |
| 1896 | state->fCurrIndex = index + 1; |
| 1897 | return true; |
| 1898 | } |
| 1899 | |
| 1900 | bool VertState::TriangleFanX(VertState* state) { |
| 1901 | const uint16_t* indices = state->fIndices; |
| 1902 | int index = state->fCurrIndex; |
| 1903 | if (index + 3 > state->fCount) { |
| 1904 | return false; |
| 1905 | } |
| 1906 | state->f0 = indices[0]; |
| 1907 | state->f1 = indices[index + 1]; |
| 1908 | state->f2 = indices[index + 2]; |
| 1909 | state->fCurrIndex = index + 1; |
| 1910 | return true; |
| 1911 | } |
| 1912 | |
| 1913 | VertState::Proc VertState::chooseProc(SkCanvas::VertexMode mode) { |
| 1914 | switch (mode) { |
| 1915 | case SkCanvas::kTriangles_VertexMode: |
| 1916 | return fIndices ? TrianglesX : Triangles; |
| 1917 | case SkCanvas::kTriangleStrip_VertexMode: |
| 1918 | return fIndices ? TriangleStripX : TriangleStrip; |
| 1919 | case SkCanvas::kTriangleFan_VertexMode: |
| 1920 | return fIndices ? TriangleFanX : TriangleFan; |
| 1921 | default: |
| 1922 | return NULL; |
| 1923 | } |
| 1924 | } |
| 1925 | |
| 1926 | typedef void (*HairProc)(const SkPoint&, const SkPoint&, const SkRegion*, |
| 1927 | SkBlitter*); |
| 1928 | |
| 1929 | static HairProc ChooseHairProc(bool doAntiAlias) { |
| 1930 | return doAntiAlias ? SkScan::AntiHairLine : SkScan::HairLine; |
| 1931 | } |
| 1932 | |
| 1933 | static bool texture_to_matrix(const VertState& state, const SkPoint verts[], |
| 1934 | const SkPoint texs[], SkMatrix* matrix) { |
| 1935 | SkPoint src[3], dst[3]; |
| 1936 | |
| 1937 | src[0] = texs[state.f0]; |
| 1938 | src[1] = texs[state.f1]; |
| 1939 | src[2] = texs[state.f2]; |
| 1940 | dst[0] = verts[state.f0]; |
| 1941 | dst[1] = verts[state.f1]; |
| 1942 | dst[2] = verts[state.f2]; |
| 1943 | return matrix->setPolyToPoly(src, dst, 3); |
| 1944 | } |
| 1945 | |
| 1946 | class SkTriColorShader : public SkShader { |
| 1947 | public: |
| 1948 | SkTriColorShader() {} |
| 1949 | |
| 1950 | bool setup(const SkPoint pts[], const SkColor colors[], int, int, int); |
| 1951 | |
| 1952 | virtual void shadeSpan(int x, int y, SkPMColor dstC[], int count); |
| 1953 | |
| 1954 | protected: |
| 1955 | SkTriColorShader(SkFlattenableReadBuffer& buffer) : SkShader(buffer) {} |
| 1956 | |
| 1957 | virtual Factory getFactory() { return CreateProc; } |
| 1958 | |
| 1959 | private: |
| 1960 | SkMatrix fDstToUnit; |
| 1961 | SkPMColor fColors[3]; |
| 1962 | |
| 1963 | static SkFlattenable* CreateProc(SkFlattenableReadBuffer& buffer) { |
| 1964 | return SkNEW_ARGS(SkTriColorShader, (buffer)); |
| 1965 | } |
| 1966 | typedef SkShader INHERITED; |
| 1967 | }; |
| 1968 | |
| 1969 | bool SkTriColorShader::setup(const SkPoint pts[], const SkColor colors[], |
| 1970 | int index0, int index1, int index2) { |
| 1971 | |
| 1972 | fColors[0] = SkPreMultiplyColor(colors[index0]); |
| 1973 | fColors[1] = SkPreMultiplyColor(colors[index1]); |
| 1974 | fColors[2] = SkPreMultiplyColor(colors[index2]); |
| 1975 | |
| 1976 | SkMatrix m, im; |
| 1977 | m.reset(); |
| 1978 | m.set(0, pts[index1].fX - pts[index0].fX); |
| 1979 | m.set(1, pts[index2].fX - pts[index0].fX); |
| 1980 | m.set(2, pts[index0].fX); |
| 1981 | m.set(3, pts[index1].fY - pts[index0].fY); |
| 1982 | m.set(4, pts[index2].fY - pts[index0].fY); |
| 1983 | m.set(5, pts[index0].fY); |
| 1984 | if (!m.invert(&im)) { |
| 1985 | return false; |
| 1986 | } |
| 1987 | return fDstToUnit.setConcat(im, this->getTotalInverse()); |
| 1988 | } |
| 1989 | |
| 1990 | #include "SkColorPriv.h" |
| 1991 | #include "SkPorterDuff.h" |
| 1992 | #include "SkComposShader.h" |
| 1993 | #include "SkXfermode.h" |
| 1994 | |
| 1995 | static int ScalarTo256(SkScalar v) { |
| 1996 | int scale = SkScalarToFixed(v) >> 8; |
| 1997 | if (scale < 0) { |
| 1998 | scale = 0; |
| 1999 | } |
| 2000 | if (scale > 255) { |
| 2001 | scale = 255; |
| 2002 | } |
| 2003 | return SkAlpha255To256(scale); |
| 2004 | } |
| 2005 | |
| 2006 | void SkTriColorShader::shadeSpan(int x, int y, SkPMColor dstC[], int count) { |
| 2007 | SkPoint src; |
| 2008 | |
| 2009 | for (int i = 0; i < count; i++) { |
| 2010 | fDstToUnit.mapXY(SkIntToScalar(x), SkIntToScalar(y), &src); |
| 2011 | x += 1; |
| 2012 | |
| 2013 | int scale1 = ScalarTo256(src.fX); |
| 2014 | int scale2 = ScalarTo256(src.fY); |
| 2015 | int scale0 = 256 - scale1 - scale2; |
| 2016 | if (scale0 < 0) { |
| 2017 | if (scale1 > scale2) { |
| 2018 | scale2 = 256 - scale1; |
| 2019 | } else { |
| 2020 | scale1 = 256 - scale2; |
| 2021 | } |
| 2022 | scale0 = 0; |
| 2023 | } |
| 2024 | |
| 2025 | dstC[i] = SkAlphaMulQ(fColors[0], scale0) + |
| 2026 | SkAlphaMulQ(fColors[1], scale1) + |
| 2027 | SkAlphaMulQ(fColors[2], scale2); |
| 2028 | } |
| 2029 | } |
| 2030 | |
| 2031 | void SkDraw::drawVertices(SkCanvas::VertexMode vmode, int count, |
| 2032 | const SkPoint vertices[], const SkPoint textures[], |
| 2033 | const SkColor colors[], SkXfermode* xmode, |
| 2034 | const uint16_t indices[], int indexCount, |
| 2035 | const SkPaint& paint) const { |
| 2036 | SkASSERT(0 == count || NULL != vertices); |
| 2037 | |
| 2038 | // abort early if there is nothing to draw |
| 2039 | if (count < 3 || (indices && indexCount < 3) || fClip->isEmpty() || |
| 2040 | (paint.getAlpha() == 0 && paint.getXfermode() == NULL)) { |
| 2041 | return; |
| 2042 | } |
| 2043 | |
| 2044 | // transform out vertices into device coordinates |
| 2045 | SkAutoSTMalloc<16, SkPoint> storage(count); |
| 2046 | SkPoint* devVerts = storage.get(); |
| 2047 | fMatrix->mapPoints(devVerts, vertices, count); |
| 2048 | |
| 2049 | if (fBounder) { |
| 2050 | SkRect bounds; |
| 2051 | bounds.set(devVerts, count); |
| 2052 | if (!fBounder->doRect(bounds, paint)) { |
| 2053 | return; |
| 2054 | } |
| 2055 | } |
| 2056 | |
| 2057 | /* |
| 2058 | We can draw the vertices in 1 of 4 ways: |
| 2059 | |
| 2060 | - solid color (no shader/texture[], no colors[]) |
| 2061 | - just colors (no shader/texture[], has colors[]) |
| 2062 | - just texture (has shader/texture[], no colors[]) |
| 2063 | - colors * texture (has shader/texture[], has colors[]) |
| 2064 | |
| 2065 | Thus for texture drawing, we need both texture[] and a shader. |
| 2066 | */ |
| 2067 | |
| 2068 | SkTriColorShader triShader; // must be above declaration of p |
| 2069 | SkPaint p(paint); |
| 2070 | |
| 2071 | SkShader* shader = p.getShader(); |
| 2072 | if (NULL == shader) { |
| 2073 | // if we have no shader, we ignore the texture coordinates |
| 2074 | textures = NULL; |
| 2075 | } else if (NULL == textures) { |
| 2076 | // if we don't have texture coordinates, ignore the shader |
| 2077 | p.setShader(NULL); |
| 2078 | shader = NULL; |
| 2079 | } |
| 2080 | |
| 2081 | // setup the custom shader (if needed) |
| 2082 | if (NULL != colors) { |
| 2083 | if (NULL == textures) { |
| 2084 | // just colors (no texture) |
| 2085 | p.setShader(&triShader); |
| 2086 | } else { |
| 2087 | // colors * texture |
| 2088 | SkASSERT(shader); |
| 2089 | bool releaseMode = false; |
| 2090 | if (NULL == xmode) { |
| 2091 | xmode = SkPorterDuff::CreateXfermode( |
| 2092 | SkPorterDuff::kMultiply_Mode); |
| 2093 | releaseMode = true; |
| 2094 | } |
| 2095 | SkShader* compose = SkNEW_ARGS(SkComposeShader, |
| 2096 | (&triShader, shader, xmode)); |
| 2097 | p.setShader(compose)->unref(); |
| 2098 | if (releaseMode) { |
| 2099 | xmode->unref(); |
| 2100 | } |
| 2101 | } |
| 2102 | } |
| 2103 | |
| 2104 | SkAutoBlitterChoose blitter(*fBitmap, *fMatrix, p); |
| 2105 | // setup our state and function pointer for iterating triangles |
| 2106 | VertState state(count, indices, indexCount); |
| 2107 | VertState::Proc vertProc = state.chooseProc(vmode); |
| 2108 | |
| 2109 | if (NULL != textures || NULL != colors) { |
| 2110 | SkMatrix localM, tempM; |
| 2111 | bool hasLocalM = shader && shader->getLocalMatrix(&localM); |
| 2112 | |
| 2113 | if (NULL != colors) { |
| 2114 | if (!triShader.setContext(*fBitmap, p, *fMatrix)) { |
| 2115 | colors = NULL; |
| 2116 | } |
| 2117 | } |
| 2118 | |
| 2119 | while (vertProc(&state)) { |
| 2120 | if (NULL != textures) { |
| 2121 | if (texture_to_matrix(state, vertices, textures, &tempM)) { |
| 2122 | if (hasLocalM) { |
| 2123 | tempM.postConcat(localM); |
| 2124 | } |
| 2125 | shader->setLocalMatrix(tempM); |
| 2126 | // need to recal setContext since we changed the local matrix |
| 2127 | if (!shader->setContext(*fBitmap, p, *fMatrix)) { |
| 2128 | continue; |
| 2129 | } |
| 2130 | } |
| 2131 | } |
| 2132 | if (NULL != colors) { |
| 2133 | if (!triShader.setup(vertices, colors, |
| 2134 | state.f0, state.f1, state.f2)) { |
| 2135 | continue; |
| 2136 | } |
| 2137 | } |
| 2138 | SkScan::FillTriangle(devVerts[state.f0], devVerts[state.f1], |
| 2139 | devVerts[state.f2], fClip, blitter.get()); |
| 2140 | } |
| 2141 | // now restore the shader's original local matrix |
| 2142 | if (NULL != shader) { |
| 2143 | if (hasLocalM) { |
| 2144 | shader->setLocalMatrix(localM); |
| 2145 | } else { |
| 2146 | shader->resetLocalMatrix(); |
| 2147 | } |
| 2148 | } |
| 2149 | } else { |
| 2150 | // no colors[] and no texture |
| 2151 | HairProc hairProc = ChooseHairProc(paint.isAntiAlias()); |
| 2152 | while (vertProc(&state)) { |
| 2153 | hairProc(devVerts[state.f0], devVerts[state.f1], fClip, blitter.get()); |
| 2154 | hairProc(devVerts[state.f1], devVerts[state.f2], fClip, blitter.get()); |
| 2155 | hairProc(devVerts[state.f2], devVerts[state.f0], fClip, blitter.get()); |
| 2156 | } |
| 2157 | } |
| 2158 | } |
| 2159 | |
| 2160 | //////////////////////////////////////////////////////////////////////////////////////// |
| 2161 | //////////////////////////////////////////////////////////////////////////////////////// |
| 2162 | |
| 2163 | #ifdef SK_DEBUG |
| 2164 | |
| 2165 | void SkDraw::validate() const { |
| 2166 | SkASSERT(fBitmap != NULL); |
| 2167 | SkASSERT(fMatrix != NULL); |
| 2168 | SkASSERT(fClip != NULL); |
| 2169 | |
| 2170 | const SkIRect& cr = fClip->getBounds(); |
| 2171 | SkIRect br; |
| 2172 | |
| 2173 | br.set(0, 0, fBitmap->width(), fBitmap->height()); |
| 2174 | SkASSERT(cr.isEmpty() || br.contains(cr)); |
| 2175 | } |
| 2176 | |
| 2177 | #endif |
| 2178 | |
| 2179 | ////////////////////////////////////////////////////////////////////////////////////////// |
| 2180 | |
| 2181 | bool SkBounder::doIRect(const SkIRect& r) { |
| 2182 | SkIRect rr; |
| 2183 | return rr.intersect(fClip->getBounds(), r) && this->onIRect(rr); |
| 2184 | } |
| 2185 | |
| 2186 | bool SkBounder::doHairline(const SkPoint& pt0, const SkPoint& pt1, |
| 2187 | const SkPaint& paint) { |
| 2188 | SkIRect r; |
| 2189 | SkScalar v0, v1; |
| 2190 | |
| 2191 | v0 = pt0.fX; |
| 2192 | v1 = pt1.fX; |
| 2193 | if (v0 > v1) { |
| 2194 | SkTSwap<SkScalar>(v0, v1); |
| 2195 | } |
| 2196 | r.fLeft = SkScalarFloor(v0); |
| 2197 | r.fRight = SkScalarCeil(v1); |
| 2198 | |
| 2199 | v0 = pt0.fY; |
| 2200 | v1 = pt1.fY; |
| 2201 | if (v0 > v1) { |
| 2202 | SkTSwap<SkScalar>(v0, v1); |
| 2203 | } |
| 2204 | r.fTop = SkScalarFloor(v0); |
| 2205 | r.fBottom = SkScalarCeil(v1); |
| 2206 | |
| 2207 | if (paint.isAntiAlias()) { |
| 2208 | r.inset(-1, -1); |
| 2209 | } |
| 2210 | return this->doIRect(r); |
| 2211 | } |
| 2212 | |
| 2213 | bool SkBounder::doRect(const SkRect& rect, const SkPaint& paint) { |
| 2214 | SkIRect r; |
| 2215 | |
| 2216 | if (paint.getStyle() == SkPaint::kFill_Style) { |
| 2217 | rect.round(&r); |
| 2218 | } else { |
| 2219 | int rad = -1; |
| 2220 | rect.roundOut(&r); |
| 2221 | if (paint.isAntiAlias()) { |
| 2222 | rad = -2; |
| 2223 | } |
| 2224 | r.inset(rad, rad); |
| 2225 | } |
| 2226 | return this->doIRect(r); |
| 2227 | } |
| 2228 | |
| 2229 | bool SkBounder::doPath(const SkPath& path, const SkPaint& paint, bool doFill) { |
| 2230 | SkRect bounds; |
| 2231 | SkIRect r; |
| 2232 | |
| 2233 | path.computeBounds(&bounds, SkPath::kFast_BoundsType); |
| 2234 | |
| 2235 | if (doFill) { |
| 2236 | bounds.round(&r); |
| 2237 | } else { // hairline |
| 2238 | bounds.roundOut(&r); |
| 2239 | } |
| 2240 | |
| 2241 | if (paint.isAntiAlias()) { |
| 2242 | r.inset(-1, -1); |
| 2243 | } |
| 2244 | return this->doIRect(r); |
| 2245 | } |
| 2246 | |
| 2247 | void SkBounder::commit() { |
| 2248 | // override in subclass |
| 2249 | } |
| 2250 | |
| 2251 | //////////////////////////////////////////////////////////////////////////////////////////////// |
| 2252 | |
| 2253 | #include "SkPath.h" |
| 2254 | #include "SkDraw.h" |
| 2255 | #include "SkRegion.h" |
| 2256 | #include "SkBlitter.h" |
| 2257 | |
| 2258 | static bool compute_bounds(const SkPath& devPath, const SkIRect* clipBounds, |
| 2259 | SkMaskFilter* filter, const SkMatrix* filterMatrix, |
| 2260 | SkIRect* bounds) { |
| 2261 | if (devPath.isEmpty()) { |
| 2262 | return false; |
| 2263 | } |
| 2264 | |
| 2265 | SkIPoint margin; |
| 2266 | margin.set(0, 0); |
| 2267 | |
| 2268 | // init our bounds from the path |
| 2269 | { |
| 2270 | SkRect pathBounds; |
| 2271 | devPath.computeBounds(&pathBounds, SkPath::kExact_BoundsType); |
| 2272 | pathBounds.inset(-SK_ScalarHalf, -SK_ScalarHalf); |
| 2273 | pathBounds.roundOut(bounds); |
| 2274 | } |
| 2275 | |
| 2276 | if (filter) { |
| 2277 | SkASSERT(filterMatrix); |
| 2278 | |
| 2279 | SkMask srcM, dstM; |
| 2280 | |
| 2281 | srcM.fBounds = *bounds; |
| 2282 | srcM.fFormat = SkMask::kA8_Format; |
| 2283 | srcM.fImage = NULL; |
| 2284 | if (!filter->filterMask(&dstM, srcM, *filterMatrix, &margin)) { |
| 2285 | return false; |
| 2286 | } |
| 2287 | *bounds = dstM.fBounds; |
| 2288 | } |
| 2289 | |
| 2290 | if (clipBounds && !SkIRect::Intersects(*clipBounds, *bounds)) { |
| 2291 | return false; |
| 2292 | } |
| 2293 | |
| 2294 | // (possibly) trim the srcM bounds to reflect the clip |
| 2295 | // (plus whatever slop the filter needs) |
| 2296 | if (clipBounds && !clipBounds->contains(*bounds)) { |
| 2297 | SkIRect tmp = *bounds; |
| 2298 | (void)tmp.intersect(*clipBounds); |
| 2299 | tmp.inset(-margin.fX, -margin.fY); |
| 2300 | (void)bounds->intersect(tmp); |
| 2301 | } |
| 2302 | |
| 2303 | return true; |
| 2304 | } |
| 2305 | |
| 2306 | static void draw_into_mask(const SkMask& mask, const SkPath& devPath) { |
| 2307 | SkBitmap bm; |
| 2308 | SkDraw draw; |
| 2309 | SkRegion clipRgn; |
| 2310 | SkMatrix matrix; |
| 2311 | SkPaint paint; |
| 2312 | |
| 2313 | bm.setConfig(SkBitmap::kA8_Config, mask.fBounds.width(), mask.fBounds.height(), mask.fRowBytes); |
| 2314 | bm.setPixels(mask.fImage); |
| 2315 | |
| 2316 | clipRgn.setRect(0, 0, mask.fBounds.width(), mask.fBounds.height()); |
| 2317 | matrix.setTranslate(-SkIntToScalar(mask.fBounds.fLeft), |
| 2318 | -SkIntToScalar(mask.fBounds.fTop)); |
| 2319 | |
| 2320 | draw.fBitmap = &bm; |
| 2321 | draw.fClip = &clipRgn; |
| 2322 | draw.fMatrix = &matrix; |
| 2323 | draw.fBounder = NULL; |
| 2324 | paint.setAntiAlias(true); |
| 2325 | draw.drawPath(devPath, paint); |
| 2326 | } |
| 2327 | |
| 2328 | bool SkDraw::DrawToMask(const SkPath& devPath, const SkIRect* clipBounds, |
| 2329 | SkMaskFilter* filter, const SkMatrix* filterMatrix, |
| 2330 | SkMask* mask, SkMask::CreateMode mode) { |
| 2331 | if (SkMask::kJustRenderImage_CreateMode != mode) { |
| 2332 | if (!compute_bounds(devPath, clipBounds, filter, filterMatrix, &mask->fBounds)) |
| 2333 | return false; |
| 2334 | } |
| 2335 | |
| 2336 | if (SkMask::kComputeBoundsAndRenderImage_CreateMode == mode) { |
| 2337 | mask->fFormat = SkMask::kA8_Format; |
| 2338 | mask->fRowBytes = mask->fBounds.width(); |
| 2339 | mask->fImage = SkMask::AllocImage(mask->computeImageSize()); |
| 2340 | memset(mask->fImage, 0, mask->computeImageSize()); |
| 2341 | } |
| 2342 | |
| 2343 | if (SkMask::kJustComputeBounds_CreateMode != mode) { |
| 2344 | draw_into_mask(*mask, devPath); |
| 2345 | } |
| 2346 | |
| 2347 | return true; |
| 2348 | } |