michael@0: michael@0: /* michael@0: * Copyright 2006 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: michael@0: #include "SkBlitter.h" michael@0: #include "SkAntiRun.h" michael@0: #include "SkColor.h" michael@0: #include "SkColorFilter.h" michael@0: #include "SkCoreBlitters.h" michael@0: #include "SkFilterShader.h" michael@0: #include "SkReadBuffer.h" michael@0: #include "SkWriteBuffer.h" michael@0: #include "SkMask.h" michael@0: #include "SkMaskFilter.h" michael@0: #include "SkString.h" michael@0: #include "SkTLazy.h" michael@0: #include "SkUtils.h" michael@0: #include "SkXfermode.h" michael@0: michael@0: SkBlitter::~SkBlitter() {} michael@0: michael@0: bool SkBlitter::isNullBlitter() const { return false; } michael@0: michael@0: const SkBitmap* SkBlitter::justAnOpaqueColor(uint32_t* value) { michael@0: return NULL; michael@0: } michael@0: michael@0: void SkBlitter::blitH(int x, int y, int width) { michael@0: SkDEBUGFAIL("unimplemented"); michael@0: } michael@0: michael@0: void SkBlitter::blitAntiH(int x, int y, const SkAlpha antialias[], michael@0: const int16_t runs[]) { michael@0: SkDEBUGFAIL("unimplemented"); michael@0: } michael@0: michael@0: void SkBlitter::blitV(int x, int y, int height, SkAlpha alpha) { michael@0: if (alpha == 255) { michael@0: this->blitRect(x, y, 1, height); michael@0: } else { michael@0: int16_t runs[2]; michael@0: runs[0] = 1; michael@0: runs[1] = 0; michael@0: michael@0: while (--height >= 0) { michael@0: this->blitAntiH(x, y++, &alpha, runs); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void SkBlitter::blitRect(int x, int y, int width, int height) { michael@0: SkASSERT(width > 0); michael@0: while (--height >= 0) { michael@0: this->blitH(x, y++, width); michael@0: } michael@0: } michael@0: michael@0: /// Default implementation doesn't check for any easy optimizations michael@0: /// such as alpha == 0 or 255; also uses blitV(), which some subclasses michael@0: /// may not support. michael@0: void SkBlitter::blitAntiRect(int x, int y, int width, int height, michael@0: SkAlpha leftAlpha, SkAlpha rightAlpha) { michael@0: this->blitV(x++, y, height, leftAlpha); michael@0: if (width > 0) { michael@0: this->blitRect(x, y, width, height); michael@0: x += width; michael@0: } michael@0: this->blitV(x, y, height, rightAlpha); michael@0: } michael@0: michael@0: ////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: static inline void bits_to_runs(SkBlitter* blitter, int x, int y, michael@0: const uint8_t bits[], michael@0: U8CPU left_mask, int rowBytes, michael@0: U8CPU right_mask) { michael@0: int inFill = 0; michael@0: int pos = 0; michael@0: michael@0: while (--rowBytes >= 0) { michael@0: unsigned b = *bits++ & left_mask; michael@0: if (rowBytes == 0) { michael@0: b &= right_mask; michael@0: } michael@0: michael@0: for (unsigned test = 0x80; test != 0; test >>= 1) { michael@0: if (b & test) { michael@0: if (!inFill) { michael@0: pos = x; michael@0: inFill = true; michael@0: } michael@0: } else { michael@0: if (inFill) { michael@0: blitter->blitH(pos, y, x - pos); michael@0: inFill = false; michael@0: } michael@0: } michael@0: x += 1; michael@0: } michael@0: left_mask = 0xFF; michael@0: } michael@0: michael@0: // final cleanup michael@0: if (inFill) { michael@0: blitter->blitH(pos, y, x - pos); michael@0: } michael@0: } michael@0: michael@0: void SkBlitter::blitMask(const SkMask& mask, const SkIRect& clip) { michael@0: SkASSERT(mask.fBounds.contains(clip)); michael@0: michael@0: if (mask.fFormat == SkMask::kBW_Format) { michael@0: int cx = clip.fLeft; michael@0: int cy = clip.fTop; michael@0: int maskLeft = mask.fBounds.fLeft; michael@0: int mask_rowBytes = mask.fRowBytes; michael@0: int height = clip.height(); michael@0: michael@0: const uint8_t* bits = mask.getAddr1(cx, cy); michael@0: michael@0: if (cx == maskLeft && clip.fRight == mask.fBounds.fRight) { michael@0: while (--height >= 0) { michael@0: bits_to_runs(this, cx, cy, bits, 0xFF, mask_rowBytes, 0xFF); michael@0: bits += mask_rowBytes; michael@0: cy += 1; michael@0: } michael@0: } else { michael@0: int left_edge = cx - maskLeft; michael@0: SkASSERT(left_edge >= 0); michael@0: int rite_edge = clip.fRight - maskLeft; michael@0: SkASSERT(rite_edge > left_edge); michael@0: michael@0: int left_mask = 0xFF >> (left_edge & 7); michael@0: int rite_mask = 0xFF << (8 - (rite_edge & 7)); michael@0: int full_runs = (rite_edge >> 3) - ((left_edge + 7) >> 3); michael@0: michael@0: // check for empty right mask, so we don't read off the end (or go slower than we need to) michael@0: if (rite_mask == 0) { michael@0: SkASSERT(full_runs >= 0); michael@0: full_runs -= 1; michael@0: rite_mask = 0xFF; michael@0: } michael@0: if (left_mask == 0xFF) { michael@0: full_runs -= 1; michael@0: } michael@0: michael@0: // back up manually so we can keep in sync with our byte-aligned src michael@0: // have cx reflect our actual starting x-coord michael@0: cx -= left_edge & 7; michael@0: michael@0: if (full_runs < 0) { michael@0: SkASSERT((left_mask & rite_mask) != 0); michael@0: while (--height >= 0) { michael@0: bits_to_runs(this, cx, cy, bits, left_mask, 1, rite_mask); michael@0: bits += mask_rowBytes; michael@0: cy += 1; michael@0: } michael@0: } else { michael@0: while (--height >= 0) { michael@0: bits_to_runs(this, cx, cy, bits, left_mask, full_runs + 2, rite_mask); michael@0: bits += mask_rowBytes; michael@0: cy += 1; michael@0: } michael@0: } michael@0: } michael@0: } else { michael@0: int width = clip.width(); michael@0: SkAutoSTMalloc<64, int16_t> runStorage(width + 1); michael@0: int16_t* runs = runStorage.get(); michael@0: const uint8_t* aa = mask.getAddr8(clip.fLeft, clip.fTop); michael@0: michael@0: sk_memset16((uint16_t*)runs, 1, width); michael@0: runs[width] = 0; michael@0: michael@0: int height = clip.height(); michael@0: int y = clip.fTop; michael@0: while (--height >= 0) { michael@0: this->blitAntiH(clip.fLeft, y, aa, runs); michael@0: aa += mask.fRowBytes; michael@0: y += 1; michael@0: } michael@0: } michael@0: } michael@0: michael@0: /////////////////////// these guys are not virtual, just a helpers michael@0: michael@0: void SkBlitter::blitMaskRegion(const SkMask& mask, const SkRegion& clip) { michael@0: if (clip.quickReject(mask.fBounds)) { michael@0: return; michael@0: } michael@0: michael@0: SkRegion::Cliperator clipper(clip, mask.fBounds); michael@0: michael@0: while (!clipper.done()) { michael@0: const SkIRect& cr = clipper.rect(); michael@0: this->blitMask(mask, cr); michael@0: clipper.next(); michael@0: } michael@0: } michael@0: michael@0: void SkBlitter::blitRectRegion(const SkIRect& rect, const SkRegion& clip) { michael@0: SkRegion::Cliperator clipper(clip, rect); michael@0: michael@0: while (!clipper.done()) { michael@0: const SkIRect& cr = clipper.rect(); michael@0: this->blitRect(cr.fLeft, cr.fTop, cr.width(), cr.height()); michael@0: clipper.next(); michael@0: } michael@0: } michael@0: michael@0: void SkBlitter::blitRegion(const SkRegion& clip) { michael@0: SkRegion::Iterator iter(clip); michael@0: michael@0: while (!iter.done()) { michael@0: const SkIRect& cr = iter.rect(); michael@0: this->blitRect(cr.fLeft, cr.fTop, cr.width(), cr.height()); michael@0: iter.next(); michael@0: } michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: void SkNullBlitter::blitH(int x, int y, int width) {} michael@0: michael@0: void SkNullBlitter::blitAntiH(int x, int y, const SkAlpha antialias[], michael@0: const int16_t runs[]) {} michael@0: michael@0: void SkNullBlitter::blitV(int x, int y, int height, SkAlpha alpha) {} michael@0: michael@0: void SkNullBlitter::blitRect(int x, int y, int width, int height) {} michael@0: michael@0: void SkNullBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {} michael@0: michael@0: const SkBitmap* SkNullBlitter::justAnOpaqueColor(uint32_t* value) { michael@0: return NULL; michael@0: } michael@0: michael@0: bool SkNullBlitter::isNullBlitter() const { return true; } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: static int compute_anti_width(const int16_t runs[]) { michael@0: int width = 0; michael@0: michael@0: for (;;) { michael@0: int count = runs[0]; michael@0: michael@0: SkASSERT(count >= 0); michael@0: if (count == 0) { michael@0: break; michael@0: } michael@0: width += count; michael@0: runs += count; michael@0: } michael@0: return width; michael@0: } michael@0: michael@0: static inline bool y_in_rect(int y, const SkIRect& rect) { michael@0: return (unsigned)(y - rect.fTop) < (unsigned)rect.height(); michael@0: } michael@0: michael@0: static inline bool x_in_rect(int x, const SkIRect& rect) { michael@0: return (unsigned)(x - rect.fLeft) < (unsigned)rect.width(); michael@0: } michael@0: michael@0: void SkRectClipBlitter::blitH(int left, int y, int width) { michael@0: SkASSERT(width > 0); michael@0: michael@0: if (!y_in_rect(y, fClipRect)) { michael@0: return; michael@0: } michael@0: michael@0: int right = left + width; michael@0: michael@0: if (left < fClipRect.fLeft) { michael@0: left = fClipRect.fLeft; michael@0: } michael@0: if (right > fClipRect.fRight) { michael@0: right = fClipRect.fRight; michael@0: } michael@0: michael@0: width = right - left; michael@0: if (width > 0) { michael@0: fBlitter->blitH(left, y, width); michael@0: } michael@0: } michael@0: michael@0: void SkRectClipBlitter::blitAntiH(int left, int y, const SkAlpha aa[], michael@0: const int16_t runs[]) { michael@0: if (!y_in_rect(y, fClipRect) || left >= fClipRect.fRight) { michael@0: return; michael@0: } michael@0: michael@0: int x0 = left; michael@0: int x1 = left + compute_anti_width(runs); michael@0: michael@0: if (x1 <= fClipRect.fLeft) { michael@0: return; michael@0: } michael@0: michael@0: SkASSERT(x0 < x1); michael@0: if (x0 < fClipRect.fLeft) { michael@0: int dx = fClipRect.fLeft - x0; michael@0: SkAlphaRuns::BreakAt((int16_t*)runs, (uint8_t*)aa, dx); michael@0: runs += dx; michael@0: aa += dx; michael@0: x0 = fClipRect.fLeft; michael@0: } michael@0: michael@0: SkASSERT(x0 < x1 && runs[x1 - x0] == 0); michael@0: if (x1 > fClipRect.fRight) { michael@0: x1 = fClipRect.fRight; michael@0: SkAlphaRuns::BreakAt((int16_t*)runs, (uint8_t*)aa, x1 - x0); michael@0: ((int16_t*)runs)[x1 - x0] = 0; michael@0: } michael@0: michael@0: SkASSERT(x0 < x1 && runs[x1 - x0] == 0); michael@0: SkASSERT(compute_anti_width(runs) == x1 - x0); michael@0: michael@0: fBlitter->blitAntiH(x0, y, aa, runs); michael@0: } michael@0: michael@0: void SkRectClipBlitter::blitV(int x, int y, int height, SkAlpha alpha) { michael@0: SkASSERT(height > 0); michael@0: michael@0: if (!x_in_rect(x, fClipRect)) { michael@0: return; michael@0: } michael@0: michael@0: int y0 = y; michael@0: int y1 = y + height; michael@0: michael@0: if (y0 < fClipRect.fTop) { michael@0: y0 = fClipRect.fTop; michael@0: } michael@0: if (y1 > fClipRect.fBottom) { michael@0: y1 = fClipRect.fBottom; michael@0: } michael@0: michael@0: if (y0 < y1) { michael@0: fBlitter->blitV(x, y0, y1 - y0, alpha); michael@0: } michael@0: } michael@0: michael@0: void SkRectClipBlitter::blitRect(int left, int y, int width, int height) { michael@0: SkIRect r; michael@0: michael@0: r.set(left, y, left + width, y + height); michael@0: if (r.intersect(fClipRect)) { michael@0: fBlitter->blitRect(r.fLeft, r.fTop, r.width(), r.height()); michael@0: } michael@0: } michael@0: michael@0: void SkRectClipBlitter::blitAntiRect(int left, int y, int width, int height, michael@0: SkAlpha leftAlpha, SkAlpha rightAlpha) { michael@0: SkIRect r; michael@0: michael@0: // The *true* width of the rectangle blitted is width+2: michael@0: r.set(left, y, left + width + 2, y + height); michael@0: if (r.intersect(fClipRect)) { michael@0: if (r.fLeft != left) { michael@0: SkASSERT(r.fLeft > left); michael@0: leftAlpha = 255; michael@0: } michael@0: if (r.fRight != left + width + 2) { michael@0: SkASSERT(r.fRight < left + width + 2); michael@0: rightAlpha = 255; michael@0: } michael@0: if (255 == leftAlpha && 255 == rightAlpha) { michael@0: fBlitter->blitRect(r.fLeft, r.fTop, r.width(), r.height()); michael@0: } else if (1 == r.width()) { michael@0: if (r.fLeft == left) { michael@0: fBlitter->blitV(r.fLeft, r.fTop, r.height(), leftAlpha); michael@0: } else { michael@0: SkASSERT(r.fLeft == left + width + 1); michael@0: fBlitter->blitV(r.fLeft, r.fTop, r.height(), rightAlpha); michael@0: } michael@0: } else { michael@0: fBlitter->blitAntiRect(r.fLeft, r.fTop, r.width() - 2, r.height(), michael@0: leftAlpha, rightAlpha); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void SkRectClipBlitter::blitMask(const SkMask& mask, const SkIRect& clip) { michael@0: SkASSERT(mask.fBounds.contains(clip)); michael@0: michael@0: SkIRect r = clip; michael@0: michael@0: if (r.intersect(fClipRect)) { michael@0: fBlitter->blitMask(mask, r); michael@0: } michael@0: } michael@0: michael@0: const SkBitmap* SkRectClipBlitter::justAnOpaqueColor(uint32_t* value) { michael@0: return fBlitter->justAnOpaqueColor(value); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: void SkRgnClipBlitter::blitH(int x, int y, int width) { michael@0: SkRegion::Spanerator span(*fRgn, y, x, x + width); michael@0: int left, right; michael@0: michael@0: while (span.next(&left, &right)) { michael@0: SkASSERT(left < right); michael@0: fBlitter->blitH(left, y, right - left); michael@0: } michael@0: } michael@0: michael@0: void SkRgnClipBlitter::blitAntiH(int x, int y, const SkAlpha aa[], michael@0: const int16_t runs[]) { michael@0: int width = compute_anti_width(runs); michael@0: SkRegion::Spanerator span(*fRgn, y, x, x + width); michael@0: int left, right; michael@0: SkDEBUGCODE(const SkIRect& bounds = fRgn->getBounds();) michael@0: michael@0: int prevRite = x; michael@0: while (span.next(&left, &right)) { michael@0: SkASSERT(x <= left); michael@0: SkASSERT(left < right); michael@0: SkASSERT(left >= bounds.fLeft && right <= bounds.fRight); michael@0: michael@0: SkAlphaRuns::Break((int16_t*)runs, (uint8_t*)aa, left - x, right - left); michael@0: michael@0: // now zero before left michael@0: if (left > prevRite) { michael@0: int index = prevRite - x; michael@0: ((uint8_t*)aa)[index] = 0; // skip runs after right michael@0: ((int16_t*)runs)[index] = SkToS16(left - prevRite); michael@0: } michael@0: michael@0: prevRite = right; michael@0: } michael@0: michael@0: if (prevRite > x) { michael@0: ((int16_t*)runs)[prevRite - x] = 0; michael@0: michael@0: if (x < 0) { michael@0: int skip = runs[0]; michael@0: SkASSERT(skip >= -x); michael@0: aa += skip; michael@0: runs += skip; michael@0: x += skip; michael@0: } michael@0: fBlitter->blitAntiH(x, y, aa, runs); michael@0: } michael@0: } michael@0: michael@0: void SkRgnClipBlitter::blitV(int x, int y, int height, SkAlpha alpha) { michael@0: SkIRect bounds; michael@0: bounds.set(x, y, x + 1, y + height); michael@0: michael@0: SkRegion::Cliperator iter(*fRgn, bounds); michael@0: michael@0: while (!iter.done()) { michael@0: const SkIRect& r = iter.rect(); michael@0: SkASSERT(bounds.contains(r)); michael@0: michael@0: fBlitter->blitV(x, r.fTop, r.height(), alpha); michael@0: iter.next(); michael@0: } michael@0: } michael@0: michael@0: void SkRgnClipBlitter::blitRect(int x, int y, int width, int height) { michael@0: SkIRect bounds; michael@0: bounds.set(x, y, x + width, y + height); michael@0: michael@0: SkRegion::Cliperator iter(*fRgn, bounds); michael@0: michael@0: while (!iter.done()) { michael@0: const SkIRect& r = iter.rect(); michael@0: SkASSERT(bounds.contains(r)); michael@0: michael@0: fBlitter->blitRect(r.fLeft, r.fTop, r.width(), r.height()); michael@0: iter.next(); michael@0: } michael@0: } michael@0: michael@0: void SkRgnClipBlitter::blitAntiRect(int x, int y, int width, int height, michael@0: SkAlpha leftAlpha, SkAlpha rightAlpha) { michael@0: // The *true* width of the rectangle to blit is width + 2 michael@0: SkIRect bounds; michael@0: bounds.set(x, y, x + width + 2, y + height); michael@0: michael@0: SkRegion::Cliperator iter(*fRgn, bounds); michael@0: michael@0: while (!iter.done()) { michael@0: const SkIRect& r = iter.rect(); michael@0: SkASSERT(bounds.contains(r)); michael@0: SkASSERT(r.fLeft >= x); michael@0: SkASSERT(r.fRight <= x + width + 2); michael@0: michael@0: SkAlpha effectiveLeftAlpha = (r.fLeft == x) ? leftAlpha : 255; michael@0: SkAlpha effectiveRightAlpha = (r.fRight == x + width + 2) ? michael@0: rightAlpha : 255; michael@0: michael@0: if (255 == effectiveLeftAlpha && 255 == effectiveRightAlpha) { michael@0: fBlitter->blitRect(r.fLeft, r.fTop, r.width(), r.height()); michael@0: } else if (1 == r.width()) { michael@0: if (r.fLeft == x) { michael@0: fBlitter->blitV(r.fLeft, r.fTop, r.height(), michael@0: effectiveLeftAlpha); michael@0: } else { michael@0: SkASSERT(r.fLeft == x + width + 1); michael@0: fBlitter->blitV(r.fLeft, r.fTop, r.height(), michael@0: effectiveRightAlpha); michael@0: } michael@0: } else { michael@0: fBlitter->blitAntiRect(r.fLeft, r.fTop, r.width() - 2, r.height(), michael@0: effectiveLeftAlpha, effectiveRightAlpha); michael@0: } michael@0: iter.next(); michael@0: } michael@0: } michael@0: michael@0: michael@0: void SkRgnClipBlitter::blitMask(const SkMask& mask, const SkIRect& clip) { michael@0: SkASSERT(mask.fBounds.contains(clip)); michael@0: michael@0: SkRegion::Cliperator iter(*fRgn, clip); michael@0: const SkIRect& r = iter.rect(); michael@0: SkBlitter* blitter = fBlitter; michael@0: michael@0: while (!iter.done()) { michael@0: blitter->blitMask(mask, r); michael@0: iter.next(); michael@0: } michael@0: } michael@0: michael@0: const SkBitmap* SkRgnClipBlitter::justAnOpaqueColor(uint32_t* value) { michael@0: return fBlitter->justAnOpaqueColor(value); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: SkBlitter* SkBlitterClipper::apply(SkBlitter* blitter, const SkRegion* clip, michael@0: const SkIRect* ir) { michael@0: if (clip) { michael@0: const SkIRect& clipR = clip->getBounds(); michael@0: michael@0: if (clip->isEmpty() || (ir && !SkIRect::Intersects(clipR, *ir))) { michael@0: blitter = &fNullBlitter; michael@0: } else if (clip->isRect()) { michael@0: if (ir == NULL || !clipR.contains(*ir)) { michael@0: fRectBlitter.init(blitter, clipR); michael@0: blitter = &fRectBlitter; michael@0: } michael@0: } else { michael@0: fRgnBlitter.init(blitter, clip); michael@0: blitter = &fRgnBlitter; michael@0: } michael@0: } michael@0: return blitter; michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #include "SkColorShader.h" michael@0: #include "SkColorPriv.h" michael@0: michael@0: class Sk3DShader : public SkShader { michael@0: public: michael@0: Sk3DShader(SkShader* proxy) : fProxy(proxy) { michael@0: SkSafeRef(proxy); michael@0: fMask = NULL; michael@0: } michael@0: michael@0: virtual ~Sk3DShader() { michael@0: SkSafeUnref(fProxy); michael@0: } michael@0: michael@0: void setMask(const SkMask* mask) { fMask = mask; } michael@0: michael@0: virtual bool setContext(const SkBitmap& device, const SkPaint& paint, michael@0: const SkMatrix& matrix) SK_OVERRIDE { michael@0: if (!this->INHERITED::setContext(device, paint, matrix)) { michael@0: return false; michael@0: } michael@0: if (fProxy) { michael@0: if (!fProxy->setContext(device, paint, matrix)) { michael@0: // must keep our set/end context calls balanced michael@0: this->INHERITED::endContext(); michael@0: return false; michael@0: } michael@0: } else { michael@0: fPMColor = SkPreMultiplyColor(paint.getColor()); michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: virtual void endContext() SK_OVERRIDE { michael@0: if (fProxy) { michael@0: fProxy->endContext(); michael@0: } michael@0: this->INHERITED::endContext(); michael@0: } michael@0: michael@0: virtual void shadeSpan(int x, int y, SkPMColor span[], int count) SK_OVERRIDE { michael@0: if (fProxy) { michael@0: fProxy->shadeSpan(x, y, span, count); michael@0: } michael@0: michael@0: if (fMask == NULL) { michael@0: if (fProxy == NULL) { michael@0: sk_memset32(span, fPMColor, count); michael@0: } michael@0: return; michael@0: } michael@0: michael@0: SkASSERT(fMask->fBounds.contains(x, y)); michael@0: SkASSERT(fMask->fBounds.contains(x + count - 1, y)); michael@0: michael@0: size_t size = fMask->computeImageSize(); michael@0: const uint8_t* alpha = fMask->getAddr8(x, y); michael@0: const uint8_t* mulp = alpha + size; michael@0: const uint8_t* addp = mulp + size; michael@0: michael@0: if (fProxy) { michael@0: for (int i = 0; i < count; i++) { michael@0: if (alpha[i]) { michael@0: SkPMColor c = span[i]; michael@0: if (c) { michael@0: unsigned a = SkGetPackedA32(c); michael@0: unsigned r = SkGetPackedR32(c); michael@0: unsigned g = SkGetPackedG32(c); michael@0: unsigned b = SkGetPackedB32(c); michael@0: michael@0: unsigned mul = SkAlpha255To256(mulp[i]); michael@0: unsigned add = addp[i]; michael@0: michael@0: r = SkFastMin32(SkAlphaMul(r, mul) + add, a); michael@0: g = SkFastMin32(SkAlphaMul(g, mul) + add, a); michael@0: b = SkFastMin32(SkAlphaMul(b, mul) + add, a); michael@0: michael@0: span[i] = SkPackARGB32(a, r, g, b); michael@0: } michael@0: } else { michael@0: span[i] = 0; michael@0: } michael@0: } michael@0: } else { // color michael@0: unsigned a = SkGetPackedA32(fPMColor); michael@0: unsigned r = SkGetPackedR32(fPMColor); michael@0: unsigned g = SkGetPackedG32(fPMColor); michael@0: unsigned b = SkGetPackedB32(fPMColor); michael@0: for (int i = 0; i < count; i++) { michael@0: if (alpha[i]) { michael@0: unsigned mul = SkAlpha255To256(mulp[i]); michael@0: unsigned add = addp[i]; michael@0: michael@0: span[i] = SkPackARGB32( a, michael@0: SkFastMin32(SkAlphaMul(r, mul) + add, a), michael@0: SkFastMin32(SkAlphaMul(g, mul) + add, a), michael@0: SkFastMin32(SkAlphaMul(b, mul) + add, a)); michael@0: } else { michael@0: span[i] = 0; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: #ifndef SK_IGNORE_TO_STRING michael@0: virtual void toString(SkString* str) const SK_OVERRIDE { michael@0: str->append("Sk3DShader: ("); michael@0: michael@0: if (NULL != fProxy) { michael@0: str->append("Proxy: "); michael@0: fProxy->toString(str); michael@0: } michael@0: michael@0: this->INHERITED::toString(str); michael@0: michael@0: str->append(")"); michael@0: } michael@0: #endif michael@0: michael@0: SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Sk3DShader) michael@0: michael@0: protected: michael@0: Sk3DShader(SkReadBuffer& buffer) : INHERITED(buffer) { michael@0: fProxy = buffer.readShader(); michael@0: fPMColor = buffer.readColor(); michael@0: fMask = NULL; michael@0: } michael@0: michael@0: virtual void flatten(SkWriteBuffer& buffer) const SK_OVERRIDE { michael@0: this->INHERITED::flatten(buffer); michael@0: buffer.writeFlattenable(fProxy); michael@0: buffer.writeColor(fPMColor); michael@0: } michael@0: michael@0: private: michael@0: SkShader* fProxy; michael@0: SkPMColor fPMColor; michael@0: const SkMask* fMask; michael@0: michael@0: typedef SkShader INHERITED; michael@0: }; michael@0: michael@0: class Sk3DBlitter : public SkBlitter { michael@0: public: michael@0: Sk3DBlitter(SkBlitter* proxy, Sk3DShader* shader) michael@0: : fProxy(proxy) michael@0: , f3DShader(SkRef(shader)) michael@0: {} michael@0: michael@0: virtual void blitH(int x, int y, int width) { michael@0: fProxy->blitH(x, y, width); michael@0: } michael@0: michael@0: virtual void blitAntiH(int x, int y, const SkAlpha antialias[], michael@0: const int16_t runs[]) { michael@0: fProxy->blitAntiH(x, y, antialias, runs); michael@0: } michael@0: michael@0: virtual void blitV(int x, int y, int height, SkAlpha alpha) { michael@0: fProxy->blitV(x, y, height, alpha); michael@0: } michael@0: michael@0: virtual void blitRect(int x, int y, int width, int height) { michael@0: fProxy->blitRect(x, y, width, height); michael@0: } michael@0: michael@0: virtual void blitMask(const SkMask& mask, const SkIRect& clip) { michael@0: if (mask.fFormat == SkMask::k3D_Format) { michael@0: f3DShader->setMask(&mask); michael@0: michael@0: ((SkMask*)&mask)->fFormat = SkMask::kA8_Format; michael@0: fProxy->blitMask(mask, clip); michael@0: ((SkMask*)&mask)->fFormat = SkMask::k3D_Format; michael@0: michael@0: f3DShader->setMask(NULL); michael@0: } else { michael@0: fProxy->blitMask(mask, clip); michael@0: } michael@0: } michael@0: michael@0: private: michael@0: // fProxy is unowned. It will be deleted by SkSmallAllocator. michael@0: SkBlitter* fProxy; michael@0: SkAutoTUnref f3DShader; michael@0: }; michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #include "SkCoreBlitters.h" michael@0: michael@0: static bool just_solid_color(const SkPaint& paint) { michael@0: if (paint.getAlpha() == 0xFF && paint.getColorFilter() == NULL) { michael@0: SkShader* shader = paint.getShader(); michael@0: if (NULL == shader || michael@0: (shader->getFlags() & SkShader::kOpaqueAlpha_Flag)) { michael@0: return true; michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: /** By analyzing the paint (with an xfermode), we may decide we can take michael@0: special action. This enum lists our possible actions michael@0: */ michael@0: enum XferInterp { michael@0: kNormal_XferInterp, // no special interpretation, draw normally michael@0: kSrcOver_XferInterp, // draw as if in srcover mode michael@0: kSkipDrawing_XferInterp // draw nothing michael@0: }; michael@0: michael@0: static XferInterp interpret_xfermode(const SkPaint& paint, SkXfermode* xfer, michael@0: SkColorType deviceCT) { michael@0: SkXfermode::Mode mode; michael@0: michael@0: if (SkXfermode::AsMode(xfer, &mode)) { michael@0: switch (mode) { michael@0: case SkXfermode::kSrc_Mode: michael@0: if (just_solid_color(paint)) { michael@0: return kSrcOver_XferInterp; michael@0: } michael@0: break; michael@0: case SkXfermode::kDst_Mode: michael@0: return kSkipDrawing_XferInterp; michael@0: case SkXfermode::kSrcOver_Mode: michael@0: return kSrcOver_XferInterp; michael@0: case SkXfermode::kDstOver_Mode: michael@0: if (kRGB_565_SkColorType == deviceCT) { michael@0: return kSkipDrawing_XferInterp; michael@0: } michael@0: break; michael@0: case SkXfermode::kSrcIn_Mode: michael@0: if (kRGB_565_SkColorType == deviceCT && michael@0: just_solid_color(paint)) { michael@0: return kSrcOver_XferInterp; michael@0: } michael@0: break; michael@0: case SkXfermode::kDstIn_Mode: michael@0: if (just_solid_color(paint)) { michael@0: return kSkipDrawing_XferInterp; michael@0: } michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: return kNormal_XferInterp; michael@0: } michael@0: michael@0: SkBlitter* SkBlitter::Choose(const SkBitmap& device, michael@0: const SkMatrix& matrix, michael@0: const SkPaint& origPaint, michael@0: SkTBlitterAllocator* allocator, michael@0: bool drawCoverage) { michael@0: SkASSERT(allocator != NULL); michael@0: michael@0: SkBlitter* blitter = NULL; michael@0: michael@0: // which check, in case we're being called by a client with a dummy device michael@0: // (e.g. they have a bounder that always aborts the draw) michael@0: if (kUnknown_SkColorType == device.colorType() || michael@0: (drawCoverage && (kAlpha_8_SkColorType != device.colorType()))) { michael@0: blitter = allocator->createT(); michael@0: return blitter; michael@0: } michael@0: michael@0: SkShader* shader = origPaint.getShader(); michael@0: SkColorFilter* cf = origPaint.getColorFilter(); michael@0: SkXfermode* mode = origPaint.getXfermode(); michael@0: Sk3DShader* shader3D = NULL; michael@0: michael@0: SkTCopyOnFirstWrite paint(origPaint); michael@0: michael@0: if (origPaint.getMaskFilter() != NULL && michael@0: origPaint.getMaskFilter()->getFormat() == SkMask::k3D_Format) { michael@0: shader3D = SkNEW_ARGS(Sk3DShader, (shader)); michael@0: // we know we haven't initialized lazyPaint yet, so just do it michael@0: paint.writable()->setShader(shader3D)->unref(); michael@0: shader = shader3D; michael@0: } michael@0: michael@0: if (NULL != mode) { michael@0: switch (interpret_xfermode(*paint, mode, device.colorType())) { michael@0: case kSrcOver_XferInterp: michael@0: mode = NULL; michael@0: paint.writable()->setXfermode(NULL); michael@0: break; michael@0: case kSkipDrawing_XferInterp:{ michael@0: blitter = allocator->createT(); michael@0: return blitter; michael@0: } michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * If the xfermode is CLEAR, then we can completely ignore the installed michael@0: * color/shader/colorfilter, and just pretend we're SRC + color==0. This michael@0: * will fall into our optimizations for SRC mode. michael@0: */ michael@0: if (SkXfermode::IsMode(mode, SkXfermode::kClear_Mode)) { michael@0: SkPaint* p = paint.writable(); michael@0: shader = p->setShader(NULL); michael@0: cf = p->setColorFilter(NULL); michael@0: mode = p->setXfermodeMode(SkXfermode::kSrc_Mode); michael@0: p->setColor(0); michael@0: } michael@0: michael@0: if (NULL == shader) { michael@0: if (mode) { michael@0: // xfermodes (and filters) require shaders for our current blitters michael@0: shader = SkNEW(SkColorShader); michael@0: paint.writable()->setShader(shader)->unref(); michael@0: } else if (cf) { michael@0: // if no shader && no xfermode, we just apply the colorfilter to michael@0: // our color and move on. michael@0: SkPaint* writablePaint = paint.writable(); michael@0: writablePaint->setColor(cf->filterColor(paint->getColor())); michael@0: writablePaint->setColorFilter(NULL); michael@0: cf = NULL; michael@0: } michael@0: } michael@0: michael@0: if (cf) { michael@0: SkASSERT(shader); michael@0: shader = SkNEW_ARGS(SkFilterShader, (shader, cf)); michael@0: paint.writable()->setShader(shader)->unref(); michael@0: // blitters should ignore the presence/absence of a filter, since michael@0: // if there is one, the shader will take care of it. michael@0: } michael@0: michael@0: /* michael@0: * We need to have balanced calls to the shader: michael@0: * setContext michael@0: * endContext michael@0: * We make the first call here, in case it fails we can abort the draw. michael@0: * The endContext() call is made by the blitter (assuming setContext did michael@0: * not fail) in its destructor. michael@0: */ michael@0: if (shader && !shader->setContext(device, *paint, matrix)) { michael@0: blitter = allocator->createT(); michael@0: return blitter; michael@0: } michael@0: michael@0: michael@0: switch (device.colorType()) { michael@0: case kAlpha_8_SkColorType: michael@0: if (drawCoverage) { michael@0: SkASSERT(NULL == shader); michael@0: SkASSERT(NULL == paint->getXfermode()); michael@0: blitter = allocator->createT(device, *paint); michael@0: } else if (shader) { michael@0: blitter = allocator->createT(device, *paint); michael@0: } else { michael@0: blitter = allocator->createT(device, *paint); michael@0: } michael@0: break; michael@0: michael@0: case kRGB_565_SkColorType: michael@0: blitter = SkBlitter_ChooseD565(device, *paint, allocator); michael@0: break; michael@0: michael@0: case kPMColor_SkColorType: michael@0: if (shader) { michael@0: blitter = allocator->createT(device, *paint); michael@0: } else if (paint->getColor() == SK_ColorBLACK) { michael@0: blitter = allocator->createT(device, *paint); michael@0: } else if (paint->getAlpha() == 0xFF) { michael@0: blitter = allocator->createT(device, *paint); michael@0: } else { michael@0: blitter = allocator->createT(device, *paint); michael@0: } michael@0: break; michael@0: michael@0: default: michael@0: SkDEBUGFAIL("unsupported device config"); michael@0: blitter = allocator->createT(); michael@0: break; michael@0: } michael@0: michael@0: if (shader3D) { michael@0: SkBlitter* innerBlitter = blitter; michael@0: // innerBlitter was allocated by allocator, which will delete it. michael@0: blitter = allocator->createT(innerBlitter, shader3D); michael@0: } michael@0: return blitter; michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: const uint16_t gMask_0F0F = 0xF0F; michael@0: const uint32_t gMask_00FF00FF = 0xFF00FF; michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: SkShaderBlitter::SkShaderBlitter(const SkBitmap& device, const SkPaint& paint) michael@0: : INHERITED(device) { michael@0: fShader = paint.getShader(); michael@0: SkASSERT(fShader); michael@0: SkASSERT(fShader->setContextHasBeenCalled()); michael@0: michael@0: fShader->ref(); michael@0: fShaderFlags = fShader->getFlags(); michael@0: } michael@0: michael@0: SkShaderBlitter::~SkShaderBlitter() { michael@0: SkASSERT(fShader->setContextHasBeenCalled()); michael@0: fShader->endContext(); michael@0: fShader->unref(); michael@0: }