2011-07-28 14:26:00 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2006 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
#include "SkTransparentShader.h"
|
|
|
|
#include "SkColorPriv.h"
|
2013-01-15 20:17:47 +00:00
|
|
|
#include "SkString.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
bool SkTransparentShader::setContext(const SkBitmap& device,
|
|
|
|
const SkPaint& paint,
|
2011-04-20 11:39:28 +00:00
|
|
|
const SkMatrix& matrix) {
|
2008-12-17 15:59:43 +00:00
|
|
|
fDevice = &device;
|
|
|
|
fAlpha = paint.getAlpha();
|
|
|
|
|
|
|
|
return this->INHERITED::setContext(device, paint, matrix);
|
|
|
|
}
|
|
|
|
|
2011-04-20 11:39:28 +00:00
|
|
|
uint32_t SkTransparentShader::getFlags() {
|
2008-12-17 15:59:43 +00:00
|
|
|
uint32_t flags = this->INHERITED::getFlags();
|
|
|
|
|
2014-02-16 22:03:38 +00:00
|
|
|
switch (fDevice->colorType()) {
|
|
|
|
case kRGB_565_SkColorType:
|
2011-04-20 11:39:28 +00:00
|
|
|
flags |= kHasSpan16_Flag;
|
|
|
|
if (fAlpha == 255)
|
|
|
|
flags |= kOpaqueAlpha_Flag;
|
|
|
|
break;
|
2014-02-16 22:03:38 +00:00
|
|
|
case kPMColor_SkColorType:
|
2011-04-20 11:39:28 +00:00
|
|
|
if (fAlpha == 255 && fDevice->isOpaque())
|
|
|
|
flags |= kOpaqueAlpha_Flag;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2011-04-20 11:39:28 +00:00
|
|
|
void SkTransparentShader::shadeSpan(int x, int y, SkPMColor span[], int count) {
|
2008-12-17 15:59:43 +00:00
|
|
|
unsigned scale = SkAlpha255To256(fAlpha);
|
|
|
|
|
2014-02-16 22:03:38 +00:00
|
|
|
switch (fDevice->colorType()) {
|
|
|
|
case kPMColor_SkColorType:
|
2011-04-20 11:39:28 +00:00
|
|
|
if (scale == 256) {
|
2011-05-20 19:06:10 +00:00
|
|
|
SkPMColor* src = fDevice->getAddr32(x, y);
|
|
|
|
if (src != span) {
|
|
|
|
memcpy(span, src, count * sizeof(SkPMColor));
|
|
|
|
}
|
2011-04-20 11:39:28 +00:00
|
|
|
} else {
|
|
|
|
const SkPMColor* src = fDevice->getAddr32(x, y);
|
|
|
|
for (int i = count - 1; i >= 0; --i) {
|
|
|
|
span[i] = SkAlphaMulQ(src[i], scale);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2014-02-16 22:03:38 +00:00
|
|
|
case kRGB_565_SkColorType: {
|
2008-12-17 15:59:43 +00:00
|
|
|
const uint16_t* src = fDevice->getAddr16(x, y);
|
2011-04-20 11:39:28 +00:00
|
|
|
if (scale == 256) {
|
|
|
|
for (int i = count - 1; i >= 0; --i) {
|
2008-12-17 15:59:43 +00:00
|
|
|
span[i] = SkPixel16ToPixel32(src[i]);
|
2011-04-20 11:39:28 +00:00
|
|
|
}
|
|
|
|
} else {
|
2008-12-17 15:59:43 +00:00
|
|
|
unsigned alpha = fAlpha;
|
2011-04-20 11:39:28 +00:00
|
|
|
for (int i = count - 1; i >= 0; --i) {
|
2008-12-17 15:59:43 +00:00
|
|
|
uint16_t c = src[i];
|
|
|
|
unsigned r = SkPacked16ToR32(c);
|
|
|
|
unsigned g = SkPacked16ToG32(c);
|
|
|
|
unsigned b = SkPacked16ToB32(c);
|
|
|
|
|
|
|
|
span[i] = SkPackARGB32( alpha,
|
|
|
|
SkAlphaMul(r, scale),
|
|
|
|
SkAlphaMul(g, scale),
|
|
|
|
SkAlphaMul(b, scale));
|
|
|
|
}
|
|
|
|
}
|
2011-04-20 11:39:28 +00:00
|
|
|
break;
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2014-02-16 22:03:38 +00:00
|
|
|
case kAlpha_8_SkColorType: {
|
2008-12-17 15:59:43 +00:00
|
|
|
const uint8_t* src = fDevice->getAddr8(x, y);
|
2011-04-20 11:39:28 +00:00
|
|
|
if (scale == 256) {
|
|
|
|
for (int i = count - 1; i >= 0; --i) {
|
2008-12-17 15:59:43 +00:00
|
|
|
span[i] = SkPackARGB32(src[i], 0, 0, 0);
|
2011-04-20 11:39:28 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int i = count - 1; i >= 0; --i) {
|
2008-12-17 15:59:43 +00:00
|
|
|
span[i] = SkPackARGB32(SkAlphaMul(src[i], scale), 0, 0, 0);
|
2011-04-20 11:39:28 +00:00
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2011-04-20 11:39:28 +00:00
|
|
|
break;
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2014-02-16 22:03:38 +00:00
|
|
|
default:
|
|
|
|
SkDEBUGFAIL("colorType not supported as a destination device");
|
2011-04-20 11:39:28 +00:00
|
|
|
break;
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-20 11:39:28 +00:00
|
|
|
void SkTransparentShader::shadeSpan16(int x, int y, uint16_t span[], int count) {
|
2014-02-16 22:03:38 +00:00
|
|
|
SkASSERT(fDevice->colorType() == kRGB_565_SkColorType);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2011-05-20 19:06:10 +00:00
|
|
|
uint16_t* src = fDevice->getAddr16(x, y);
|
|
|
|
if (src != span) {
|
|
|
|
memcpy(span, src, count << 1);
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2013-01-15 20:17:47 +00:00
|
|
|
|
|
|
|
#ifdef SK_DEVELOPER
|
|
|
|
void SkTransparentShader::toString(SkString* str) const {
|
|
|
|
str->append("SkTransparentShader: (");
|
2013-01-16 07:05:56 +00:00
|
|
|
|
2013-01-15 20:17:47 +00:00
|
|
|
this->INHERITED::toString(str);
|
|
|
|
|
|
|
|
str->append(")");
|
|
|
|
}
|
|
|
|
#endif
|