2 * Copyright 2006 The Android Open Source Project
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
11 #include "SkColorFilter.h"
12 #include "SkCoreBlitters.h"
13 #include "SkFilterShader.h"
14 #include "SkReadBuffer.h"
15 #include "SkWriteBuffer.h"
17 #include "SkMaskFilter.h"
21 #include "SkXfermode.h"
23 SkBlitter::~SkBlitter() {}
25 bool SkBlitter::isNullBlitter() const { return false; }
27 bool SkBlitter::resetShaderContext(const SkShader::ContextRec&) {
31 SkShader::Context* SkBlitter::getShaderContext() const {
35 const SkBitmap* SkBlitter::justAnOpaqueColor(uint32_t* value) {
39 void SkBlitter::blitH(int x, int y, int width) {
40 SkDEBUGFAIL("unimplemented");
43 void SkBlitter::blitAntiH(int x, int y, const SkAlpha antialias[],
44 const int16_t runs[]) {
45 SkDEBUGFAIL("unimplemented");
48 void SkBlitter::blitV(int x, int y, int height, SkAlpha alpha) {
50 this->blitRect(x, y, 1, height);
56 while (--height >= 0) {
57 this->blitAntiH(x, y++, &alpha, runs);
62 void SkBlitter::blitRect(int x, int y, int width, int height) {
64 while (--height >= 0) {
65 this->blitH(x, y++, width);
69 /// Default implementation doesn't check for any easy optimizations
70 /// such as alpha == 0 or 255; also uses blitV(), which some subclasses
72 void SkBlitter::blitAntiRect(int x, int y, int width, int height,
73 SkAlpha leftAlpha, SkAlpha rightAlpha) {
74 this->blitV(x++, y, height, leftAlpha);
76 this->blitRect(x, y, width, height);
79 this->blitV(x, y, height, rightAlpha);
82 //////////////////////////////////////////////////////////////////////////////
84 static inline void bits_to_runs(SkBlitter* blitter, int x, int y,
86 U8CPU left_mask, int rowBytes,
91 while (--rowBytes >= 0) {
92 unsigned b = *bits++ & left_mask;
97 for (unsigned test = 0x80; test != 0; test >>= 1) {
105 blitter->blitH(pos, y, x - pos);
116 blitter->blitH(pos, y, x - pos);
120 void SkBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {
121 SkASSERT(mask.fBounds.contains(clip));
123 if (mask.fFormat == SkMask::kBW_Format) {
126 int maskLeft = mask.fBounds.fLeft;
127 int mask_rowBytes = mask.fRowBytes;
128 int height = clip.height();
130 const uint8_t* bits = mask.getAddr1(cx, cy);
132 if (cx == maskLeft && clip.fRight == mask.fBounds.fRight) {
133 while (--height >= 0) {
134 bits_to_runs(this, cx, cy, bits, 0xFF, mask_rowBytes, 0xFF);
135 bits += mask_rowBytes;
139 int left_edge = cx - maskLeft;
140 SkASSERT(left_edge >= 0);
141 int rite_edge = clip.fRight - maskLeft;
142 SkASSERT(rite_edge > left_edge);
144 int left_mask = 0xFF >> (left_edge & 7);
145 int rite_mask = 0xFF << (8 - (rite_edge & 7));
146 int full_runs = (rite_edge >> 3) - ((left_edge + 7) >> 3);
148 // check for empty right mask, so we don't read off the end (or go slower than we need to)
149 if (rite_mask == 0) {
150 SkASSERT(full_runs >= 0);
154 if (left_mask == 0xFF) {
158 // back up manually so we can keep in sync with our byte-aligned src
159 // have cx reflect our actual starting x-coord
163 SkASSERT((left_mask & rite_mask) != 0);
164 while (--height >= 0) {
165 bits_to_runs(this, cx, cy, bits, left_mask, 1, rite_mask);
166 bits += mask_rowBytes;
170 while (--height >= 0) {
171 bits_to_runs(this, cx, cy, bits, left_mask, full_runs + 2, rite_mask);
172 bits += mask_rowBytes;
178 int width = clip.width();
179 SkAutoSTMalloc<64, int16_t> runStorage(width + 1);
180 int16_t* runs = runStorage.get();
181 const uint8_t* aa = mask.getAddr8(clip.fLeft, clip.fTop);
183 sk_memset16((uint16_t*)runs, 1, width);
186 int height = clip.height();
188 while (--height >= 0) {
189 this->blitAntiH(clip.fLeft, y, aa, runs);
190 aa += mask.fRowBytes;
196 /////////////////////// these guys are not virtual, just a helpers
198 void SkBlitter::blitMaskRegion(const SkMask& mask, const SkRegion& clip) {
199 if (clip.quickReject(mask.fBounds)) {
203 SkRegion::Cliperator clipper(clip, mask.fBounds);
205 while (!clipper.done()) {
206 const SkIRect& cr = clipper.rect();
207 this->blitMask(mask, cr);
212 void SkBlitter::blitRectRegion(const SkIRect& rect, const SkRegion& clip) {
213 SkRegion::Cliperator clipper(clip, rect);
215 while (!clipper.done()) {
216 const SkIRect& cr = clipper.rect();
217 this->blitRect(cr.fLeft, cr.fTop, cr.width(), cr.height());
222 void SkBlitter::blitRegion(const SkRegion& clip) {
223 SkRegion::Iterator iter(clip);
225 while (!iter.done()) {
226 const SkIRect& cr = iter.rect();
227 this->blitRect(cr.fLeft, cr.fTop, cr.width(), cr.height());
232 ///////////////////////////////////////////////////////////////////////////////
234 void SkNullBlitter::blitH(int x, int y, int width) {}
236 void SkNullBlitter::blitAntiH(int x, int y, const SkAlpha antialias[],
237 const int16_t runs[]) {}
239 void SkNullBlitter::blitV(int x, int y, int height, SkAlpha alpha) {}
241 void SkNullBlitter::blitRect(int x, int y, int width, int height) {}
243 void SkNullBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {}
245 const SkBitmap* SkNullBlitter::justAnOpaqueColor(uint32_t* value) {
249 bool SkNullBlitter::isNullBlitter() const { return true; }
251 ///////////////////////////////////////////////////////////////////////////////
253 static int compute_anti_width(const int16_t runs[]) {
259 SkASSERT(count >= 0);
269 static inline bool y_in_rect(int y, const SkIRect& rect) {
270 return (unsigned)(y - rect.fTop) < (unsigned)rect.height();
273 static inline bool x_in_rect(int x, const SkIRect& rect) {
274 return (unsigned)(x - rect.fLeft) < (unsigned)rect.width();
277 void SkRectClipBlitter::blitH(int left, int y, int width) {
280 if (!y_in_rect(y, fClipRect)) {
284 int right = left + width;
286 if (left < fClipRect.fLeft) {
287 left = fClipRect.fLeft;
289 if (right > fClipRect.fRight) {
290 right = fClipRect.fRight;
293 width = right - left;
295 fBlitter->blitH(left, y, width);
299 void SkRectClipBlitter::blitAntiH(int left, int y, const SkAlpha aa[],
300 const int16_t runs[]) {
301 if (!y_in_rect(y, fClipRect) || left >= fClipRect.fRight) {
306 int x1 = left + compute_anti_width(runs);
308 if (x1 <= fClipRect.fLeft) {
313 if (x0 < fClipRect.fLeft) {
314 int dx = fClipRect.fLeft - x0;
315 SkAlphaRuns::BreakAt((int16_t*)runs, (uint8_t*)aa, dx);
318 x0 = fClipRect.fLeft;
321 SkASSERT(x0 < x1 && runs[x1 - x0] == 0);
322 if (x1 > fClipRect.fRight) {
323 x1 = fClipRect.fRight;
324 SkAlphaRuns::BreakAt((int16_t*)runs, (uint8_t*)aa, x1 - x0);
325 ((int16_t*)runs)[x1 - x0] = 0;
328 SkASSERT(x0 < x1 && runs[x1 - x0] == 0);
329 SkASSERT(compute_anti_width(runs) == x1 - x0);
331 fBlitter->blitAntiH(x0, y, aa, runs);
334 void SkRectClipBlitter::blitV(int x, int y, int height, SkAlpha alpha) {
335 SkASSERT(height > 0);
337 if (!x_in_rect(x, fClipRect)) {
344 if (y0 < fClipRect.fTop) {
347 if (y1 > fClipRect.fBottom) {
348 y1 = fClipRect.fBottom;
352 fBlitter->blitV(x, y0, y1 - y0, alpha);
356 void SkRectClipBlitter::blitRect(int left, int y, int width, int height) {
359 r.set(left, y, left + width, y + height);
360 if (r.intersect(fClipRect)) {
361 fBlitter->blitRect(r.fLeft, r.fTop, r.width(), r.height());
365 void SkRectClipBlitter::blitAntiRect(int left, int y, int width, int height,
366 SkAlpha leftAlpha, SkAlpha rightAlpha) {
369 // The *true* width of the rectangle blitted is width+2:
370 r.set(left, y, left + width + 2, y + height);
371 if (r.intersect(fClipRect)) {
372 if (r.fLeft != left) {
373 SkASSERT(r.fLeft > left);
376 if (r.fRight != left + width + 2) {
377 SkASSERT(r.fRight < left + width + 2);
380 if (255 == leftAlpha && 255 == rightAlpha) {
381 fBlitter->blitRect(r.fLeft, r.fTop, r.width(), r.height());
382 } else if (1 == r.width()) {
383 if (r.fLeft == left) {
384 fBlitter->blitV(r.fLeft, r.fTop, r.height(), leftAlpha);
386 SkASSERT(r.fLeft == left + width + 1);
387 fBlitter->blitV(r.fLeft, r.fTop, r.height(), rightAlpha);
390 fBlitter->blitAntiRect(r.fLeft, r.fTop, r.width() - 2, r.height(),
391 leftAlpha, rightAlpha);
396 void SkRectClipBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {
397 SkASSERT(mask.fBounds.contains(clip));
401 if (r.intersect(fClipRect)) {
402 fBlitter->blitMask(mask, r);
406 const SkBitmap* SkRectClipBlitter::justAnOpaqueColor(uint32_t* value) {
407 return fBlitter->justAnOpaqueColor(value);
410 ///////////////////////////////////////////////////////////////////////////////
412 void SkRgnClipBlitter::blitH(int x, int y, int width) {
413 SkRegion::Spanerator span(*fRgn, y, x, x + width);
416 while (span.next(&left, &right)) {
417 SkASSERT(left < right);
418 fBlitter->blitH(left, y, right - left);
422 void SkRgnClipBlitter::blitAntiH(int x, int y, const SkAlpha aa[],
423 const int16_t runs[]) {
424 int width = compute_anti_width(runs);
425 SkRegion::Spanerator span(*fRgn, y, x, x + width);
427 SkDEBUGCODE(const SkIRect& bounds = fRgn->getBounds();)
430 while (span.next(&left, &right)) {
432 SkASSERT(left < right);
433 SkASSERT(left >= bounds.fLeft && right <= bounds.fRight);
435 SkAlphaRuns::Break((int16_t*)runs, (uint8_t*)aa, left - x, right - left);
437 // now zero before left
438 if (left > prevRite) {
439 int index = prevRite - x;
440 ((uint8_t*)aa)[index] = 0; // skip runs after right
441 ((int16_t*)runs)[index] = SkToS16(left - prevRite);
448 ((int16_t*)runs)[prevRite - x] = 0;
452 SkASSERT(skip >= -x);
457 fBlitter->blitAntiH(x, y, aa, runs);
461 void SkRgnClipBlitter::blitV(int x, int y, int height, SkAlpha alpha) {
463 bounds.set(x, y, x + 1, y + height);
465 SkRegion::Cliperator iter(*fRgn, bounds);
467 while (!iter.done()) {
468 const SkIRect& r = iter.rect();
469 SkASSERT(bounds.contains(r));
471 fBlitter->blitV(x, r.fTop, r.height(), alpha);
476 void SkRgnClipBlitter::blitRect(int x, int y, int width, int height) {
478 bounds.set(x, y, x + width, y + height);
480 SkRegion::Cliperator iter(*fRgn, bounds);
482 while (!iter.done()) {
483 const SkIRect& r = iter.rect();
484 SkASSERT(bounds.contains(r));
486 fBlitter->blitRect(r.fLeft, r.fTop, r.width(), r.height());
491 void SkRgnClipBlitter::blitAntiRect(int x, int y, int width, int height,
492 SkAlpha leftAlpha, SkAlpha rightAlpha) {
493 // The *true* width of the rectangle to blit is width + 2
495 bounds.set(x, y, x + width + 2, y + height);
497 SkRegion::Cliperator iter(*fRgn, bounds);
499 while (!iter.done()) {
500 const SkIRect& r = iter.rect();
501 SkASSERT(bounds.contains(r));
502 SkASSERT(r.fLeft >= x);
503 SkASSERT(r.fRight <= x + width + 2);
505 SkAlpha effectiveLeftAlpha = (r.fLeft == x) ? leftAlpha : 255;
506 SkAlpha effectiveRightAlpha = (r.fRight == x + width + 2) ?
509 if (255 == effectiveLeftAlpha && 255 == effectiveRightAlpha) {
510 fBlitter->blitRect(r.fLeft, r.fTop, r.width(), r.height());
511 } else if (1 == r.width()) {
513 fBlitter->blitV(r.fLeft, r.fTop, r.height(),
516 SkASSERT(r.fLeft == x + width + 1);
517 fBlitter->blitV(r.fLeft, r.fTop, r.height(),
518 effectiveRightAlpha);
521 fBlitter->blitAntiRect(r.fLeft, r.fTop, r.width() - 2, r.height(),
522 effectiveLeftAlpha, effectiveRightAlpha);
529 void SkRgnClipBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {
530 SkASSERT(mask.fBounds.contains(clip));
532 SkRegion::Cliperator iter(*fRgn, clip);
533 const SkIRect& r = iter.rect();
534 SkBlitter* blitter = fBlitter;
536 while (!iter.done()) {
537 blitter->blitMask(mask, r);
542 const SkBitmap* SkRgnClipBlitter::justAnOpaqueColor(uint32_t* value) {
543 return fBlitter->justAnOpaqueColor(value);
546 ///////////////////////////////////////////////////////////////////////////////
548 SkBlitter* SkBlitterClipper::apply(SkBlitter* blitter, const SkRegion* clip,
551 const SkIRect& clipR = clip->getBounds();
553 if (clip->isEmpty() || (ir && !SkIRect::Intersects(clipR, *ir))) {
554 blitter = &fNullBlitter;
555 } else if (clip->isRect()) {
556 if (ir == NULL || !clipR.contains(*ir)) {
557 fRectBlitter.init(blitter, clipR);
558 blitter = &fRectBlitter;
561 fRgnBlitter.init(blitter, clip);
562 blitter = &fRgnBlitter;
568 ///////////////////////////////////////////////////////////////////////////////
570 #include "SkColorShader.h"
571 #include "SkColorPriv.h"
573 class Sk3DShader : public SkShader {
575 Sk3DShader(SkShader* proxy) : fProxy(proxy) {
579 virtual ~Sk3DShader() {
583 size_t contextSize() const SK_OVERRIDE {
584 size_t size = sizeof(Sk3DShaderContext);
586 size += fProxy->contextSize();
591 Context* onCreateContext(const ContextRec& rec, void* storage) const SK_OVERRIDE {
592 SkShader::Context* proxyContext = NULL;
594 char* proxyContextStorage = (char*) storage + sizeof(Sk3DShaderContext);
595 proxyContext = fProxy->createContext(rec, proxyContextStorage);
600 return SkNEW_PLACEMENT_ARGS(storage, Sk3DShaderContext, (*this, rec, proxyContext));
603 class Sk3DShaderContext : public SkShader::Context {
605 // Calls proxyContext's destructor but will NOT free its memory.
606 Sk3DShaderContext(const Sk3DShader& shader, const ContextRec& rec,
607 SkShader::Context* proxyContext)
608 : INHERITED(shader, rec)
610 , fProxyContext(proxyContext)
612 if (!fProxyContext) {
613 fPMColor = SkPreMultiplyColor(rec.fPaint->getColor());
617 virtual ~Sk3DShaderContext() {
619 fProxyContext->~Context();
623 void set3DMask(const SkMask* mask) SK_OVERRIDE { fMask = mask; }
625 void shadeSpan(int x, int y, SkPMColor span[], int count) SK_OVERRIDE {
627 fProxyContext->shadeSpan(x, y, span, count);
631 if (fProxyContext == NULL) {
632 sk_memset32(span, fPMColor, count);
637 SkASSERT(fMask->fBounds.contains(x, y));
638 SkASSERT(fMask->fBounds.contains(x + count - 1, y));
640 size_t size = fMask->computeImageSize();
641 const uint8_t* alpha = fMask->getAddr8(x, y);
642 const uint8_t* mulp = alpha + size;
643 const uint8_t* addp = mulp + size;
646 for (int i = 0; i < count; i++) {
648 SkPMColor c = span[i];
650 unsigned a = SkGetPackedA32(c);
651 unsigned r = SkGetPackedR32(c);
652 unsigned g = SkGetPackedG32(c);
653 unsigned b = SkGetPackedB32(c);
655 unsigned mul = SkAlpha255To256(mulp[i]);
656 unsigned add = addp[i];
658 r = SkFastMin32(SkAlphaMul(r, mul) + add, a);
659 g = SkFastMin32(SkAlphaMul(g, mul) + add, a);
660 b = SkFastMin32(SkAlphaMul(b, mul) + add, a);
662 span[i] = SkPackARGB32(a, r, g, b);
669 unsigned a = SkGetPackedA32(fPMColor);
670 unsigned r = SkGetPackedR32(fPMColor);
671 unsigned g = SkGetPackedG32(fPMColor);
672 unsigned b = SkGetPackedB32(fPMColor);
673 for (int i = 0; i < count; i++) {
675 unsigned mul = SkAlpha255To256(mulp[i]);
676 unsigned add = addp[i];
678 span[i] = SkPackARGB32( a,
679 SkFastMin32(SkAlphaMul(r, mul) + add, a),
680 SkFastMin32(SkAlphaMul(g, mul) + add, a),
681 SkFastMin32(SkAlphaMul(b, mul) + add, a));
692 // Memory is unowned, but we need to call the destructor.
693 SkShader::Context* fProxyContext;
696 typedef SkShader::Context INHERITED;
699 #ifndef SK_IGNORE_TO_STRING
700 void toString(SkString* str) const SK_OVERRIDE {
701 str->append("Sk3DShader: (");
704 str->append("Proxy: ");
705 fProxy->toString(str);
708 this->INHERITED::toString(str);
714 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Sk3DShader)
717 void flatten(SkWriteBuffer& buffer) const SK_OVERRIDE {
718 buffer.writeFlattenable(fProxy);
724 typedef SkShader INHERITED;
727 SkFlattenable* Sk3DShader::CreateProc(SkReadBuffer& buffer) {
728 SkAutoTUnref<SkShader> shader(buffer.readShader());
729 return SkNEW_ARGS(Sk3DShader, (shader));
732 class Sk3DBlitter : public SkBlitter {
734 Sk3DBlitter(SkBlitter* proxy, SkShader::Context* shaderContext)
736 , fShaderContext(shaderContext)
739 void blitH(int x, int y, int width) SK_OVERRIDE {
740 fProxy->blitH(x, y, width);
743 virtual void blitAntiH(int x, int y, const SkAlpha antialias[],
744 const int16_t runs[]) SK_OVERRIDE {
745 fProxy->blitAntiH(x, y, antialias, runs);
748 void blitV(int x, int y, int height, SkAlpha alpha) SK_OVERRIDE {
749 fProxy->blitV(x, y, height, alpha);
752 void blitRect(int x, int y, int width, int height) SK_OVERRIDE {
753 fProxy->blitRect(x, y, width, height);
756 void blitMask(const SkMask& mask, const SkIRect& clip) SK_OVERRIDE {
757 if (mask.fFormat == SkMask::k3D_Format) {
758 fShaderContext->set3DMask(&mask);
760 ((SkMask*)&mask)->fFormat = SkMask::kA8_Format;
761 fProxy->blitMask(mask, clip);
762 ((SkMask*)&mask)->fFormat = SkMask::k3D_Format;
764 fShaderContext->set3DMask(NULL);
766 fProxy->blitMask(mask, clip);
771 // Both pointers are unowned. They will be deleted by SkSmallAllocator.
773 SkShader::Context* fShaderContext;
776 ///////////////////////////////////////////////////////////////////////////////
778 #include "SkCoreBlitters.h"
780 static bool just_solid_color(const SkPaint& paint) {
781 if (paint.getAlpha() == 0xFF && paint.getColorFilter() == NULL) {
782 SkShader* shader = paint.getShader();
783 if (NULL == shader) {
790 /** By analyzing the paint (with an xfermode), we may decide we can take
791 special action. This enum lists our possible actions
794 kNormal_XferInterp, // no special interpretation, draw normally
795 kSrcOver_XferInterp, // draw as if in srcover mode
796 kSkipDrawing_XferInterp // draw nothing
799 static XferInterp interpret_xfermode(const SkPaint& paint, SkXfermode* xfer,
800 SkColorType deviceCT) {
801 SkXfermode::Mode mode;
803 if (SkXfermode::AsMode(xfer, &mode)) {
805 case SkXfermode::kSrc_Mode:
806 if (just_solid_color(paint)) {
807 return kSrcOver_XferInterp;
810 case SkXfermode::kDst_Mode:
811 return kSkipDrawing_XferInterp;
812 case SkXfermode::kSrcOver_Mode:
813 return kSrcOver_XferInterp;
814 case SkXfermode::kDstOver_Mode:
815 if (kRGB_565_SkColorType == deviceCT) {
816 return kSkipDrawing_XferInterp;
819 case SkXfermode::kSrcIn_Mode:
820 if (kRGB_565_SkColorType == deviceCT &&
821 just_solid_color(paint)) {
822 return kSrcOver_XferInterp;
825 case SkXfermode::kDstIn_Mode:
826 if (just_solid_color(paint)) {
827 return kSkipDrawing_XferInterp;
834 return kNormal_XferInterp;
837 SkBlitter* SkBlitter::Choose(const SkBitmap& device,
838 const SkMatrix& matrix,
839 const SkPaint& origPaint,
840 SkTBlitterAllocator* allocator,
842 SkASSERT(allocator != NULL);
844 // which check, in case we're being called by a client with a dummy device
845 // (e.g. they have a bounder that always aborts the draw)
846 if (kUnknown_SkColorType == device.colorType() ||
847 (drawCoverage && (kAlpha_8_SkColorType != device.colorType()))) {
848 return allocator->createT<SkNullBlitter>();
851 SkShader* shader = origPaint.getShader();
852 SkColorFilter* cf = origPaint.getColorFilter();
853 SkXfermode* mode = origPaint.getXfermode();
854 Sk3DShader* shader3D = NULL;
856 SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
858 if (origPaint.getMaskFilter() != NULL &&
859 origPaint.getMaskFilter()->getFormat() == SkMask::k3D_Format) {
860 shader3D = SkNEW_ARGS(Sk3DShader, (shader));
861 // we know we haven't initialized lazyPaint yet, so just do it
862 paint.writable()->setShader(shader3D)->unref();
867 switch (interpret_xfermode(*paint, mode, device.colorType())) {
868 case kSrcOver_XferInterp:
870 paint.writable()->setXfermode(NULL);
872 case kSkipDrawing_XferInterp:{
873 return allocator->createT<SkNullBlitter>();
881 * If the xfermode is CLEAR, then we can completely ignore the installed
882 * color/shader/colorfilter, and just pretend we're SRC + color==0. This
883 * will fall into our optimizations for SRC mode.
885 if (SkXfermode::IsMode(mode, SkXfermode::kClear_Mode)) {
886 SkPaint* p = paint.writable();
887 shader = p->setShader(NULL);
888 cf = p->setColorFilter(NULL);
889 mode = p->setXfermodeMode(SkXfermode::kSrc_Mode);
893 if (NULL == shader) {
895 // xfermodes (and filters) require shaders for our current blitters
896 shader = SkNEW_ARGS(SkColorShader, (paint->getColor()));
897 paint.writable()->setShader(shader)->unref();
898 paint.writable()->setAlpha(0xFF);
900 // if no shader && no xfermode, we just apply the colorfilter to
901 // our color and move on.
902 SkPaint* writablePaint = paint.writable();
903 writablePaint->setColor(cf->filterColor(paint->getColor()));
904 writablePaint->setColorFilter(NULL);
911 shader = SkNEW_ARGS(SkFilterShader, (shader, cf));
912 paint.writable()->setShader(shader)->unref();
913 // blitters should ignore the presence/absence of a filter, since
914 // if there is one, the shader will take care of it.
918 * We create a SkShader::Context object, and store it on the blitter.
920 SkShader::Context* shaderContext = NULL;
922 SkShader::ContextRec rec(device, *paint, matrix);
923 size_t contextSize = shader->contextSize();
925 // Try to create the ShaderContext
926 void* storage = allocator->reserveT<SkShader::Context>(contextSize);
927 shaderContext = shader->createContext(rec, storage);
928 if (!shaderContext) {
929 allocator->freeLast();
930 return allocator->createT<SkNullBlitter>();
932 SkASSERT(shaderContext);
933 SkASSERT((void*) shaderContext == storage);
935 return allocator->createT<SkNullBlitter>();
939 SkBlitter* blitter = NULL;
940 switch (device.colorType()) {
941 case kAlpha_8_SkColorType:
943 SkASSERT(NULL == shader);
944 SkASSERT(NULL == paint->getXfermode());
945 blitter = allocator->createT<SkA8_Coverage_Blitter>(device, *paint);
947 blitter = allocator->createT<SkA8_Shader_Blitter>(device, *paint, shaderContext);
949 blitter = allocator->createT<SkA8_Blitter>(device, *paint);
953 case kRGB_565_SkColorType:
954 blitter = SkBlitter_ChooseD565(device, *paint, shaderContext, allocator);
957 case kN32_SkColorType:
959 blitter = allocator->createT<SkARGB32_Shader_Blitter>(
960 device, *paint, shaderContext);
961 } else if (paint->getColor() == SK_ColorBLACK) {
962 blitter = allocator->createT<SkARGB32_Black_Blitter>(device, *paint);
963 } else if (paint->getAlpha() == 0xFF) {
964 blitter = allocator->createT<SkARGB32_Opaque_Blitter>(device, *paint);
966 blitter = allocator->createT<SkARGB32_Blitter>(device, *paint);
971 SkDEBUGFAIL("unsupported device config");
972 blitter = allocator->createT<SkNullBlitter>();
977 SkBlitter* innerBlitter = blitter;
978 // innerBlitter was allocated by allocator, which will delete it.
979 // We know shaderContext or its proxies is of type Sk3DShaderContext, so we need to
980 // wrapper the blitter to notify it when we see an emboss mask.
981 blitter = allocator->createT<Sk3DBlitter>(innerBlitter, shaderContext);
986 ///////////////////////////////////////////////////////////////////////////////
988 class SkTransparentShaderContext : public SkShader::Context {
990 SkTransparentShaderContext(const SkShader& shader, const SkShader::ContextRec& rec)
991 // Override rec with the identity matrix, so it is guaranteed to be invertible.
992 : INHERITED(shader, SkShader::ContextRec(*rec.fDevice, *rec.fPaint, SkMatrix::I())) {}
994 void shadeSpan(int x, int y, SkPMColor colors[], int count) SK_OVERRIDE {
995 sk_bzero(colors, count * sizeof(SkPMColor));
999 typedef SkShader::Context INHERITED;
1002 SkShaderBlitter::SkShaderBlitter(const SkBitmap& device, const SkPaint& paint,
1003 SkShader::Context* shaderContext)
1005 , fShader(paint.getShader())
1006 , fShaderContext(shaderContext) {
1008 SkASSERT(fShaderContext);
1011 fShaderFlags = fShaderContext->getFlags();
1014 SkShaderBlitter::~SkShaderBlitter() {
1018 bool SkShaderBlitter::resetShaderContext(const SkShader::ContextRec& rec) {
1019 // Only destroy the old context if we have a new one. We need to ensure to have a
1020 // live context in fShaderContext because the storage is owned by an SkSmallAllocator
1021 // outside of this class.
1022 // The new context will be of the same size as the old one because we use the same
1023 // shader to create it. It is therefore safe to re-use the storage.
1024 fShaderContext->~Context();
1025 SkShader::Context* ctx = fShader->createContext(rec, (void*)fShaderContext);
1027 // Need a valid context in fShaderContext's storage, so we can later (or our caller) call
1028 // the in-place destructor.
1029 SkNEW_PLACEMENT_ARGS(fShaderContext, SkTransparentShaderContext, (*fShader, rec));