#include "SkGrTexturePixelRef.h"
#include "SkBounder.h"
-#include "SkColorFilter.h"
#include "SkDeviceImageFilterProxy.h"
#include "SkDrawProcs.h"
#include "SkGlyphCache.h"
SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
-namespace {
-
-// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
-// justAlpha indicates that skPaint's alpha should be used rather than the color
-// Callers may subsequently modify the GrPaint. Setting constantColor indicates
-// that the final paint will draw the same color at every pixel. This allows
-// an optimization where the the color filter can be applied to the skPaint's
-// color once while converting to GrPaint and then ignored.
-inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
- const SkPaint& skPaint,
- bool justAlpha,
- bool constantColor,
- GrPaint* grPaint) {
-
- grPaint->setDither(skPaint.isDither());
- grPaint->setAntiAlias(skPaint.isAntiAlias());
-
- SkXfermode::Coeff sm;
- SkXfermode::Coeff dm;
-
- SkXfermode* mode = skPaint.getXfermode();
- GrEffectRef* xferEffect = NULL;
- if (SkXfermode::AsNewEffectOrCoeff(mode, &xferEffect, &sm, &dm)) {
- if (NULL != xferEffect) {
- grPaint->addColorEffect(xferEffect)->unref();
- sm = SkXfermode::kOne_Coeff;
- dm = SkXfermode::kZero_Coeff;
- }
- } else {
- //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
-#if 0
- return false;
-#else
- // Fall back to src-over
- sm = SkXfermode::kOne_Coeff;
- dm = SkXfermode::kISA_Coeff;
-#endif
- }
- grPaint->setBlendFunc(sk_blend_to_grblend(sm), sk_blend_to_grblend(dm));
-
- if (justAlpha) {
- uint8_t alpha = skPaint.getAlpha();
- grPaint->setColor(GrColorPackRGBA(alpha, alpha, alpha, alpha));
- // justAlpha is currently set to true only if there is a texture,
- // so constantColor should not also be true.
- SkASSERT(!constantColor);
- } else {
- grPaint->setColor(SkColor2GrColor(skPaint.getColor()));
- }
-
- SkColorFilter* colorFilter = skPaint.getColorFilter();
- if (NULL != colorFilter) {
- // if the source color is a constant then apply the filter here once rather than per pixel
- // in a shader.
- if (constantColor) {
- SkColor filtered = colorFilter->filterColor(skPaint.getColor());
- grPaint->setColor(SkColor2GrColor(filtered));
- } else {
- SkAutoTUnref<GrEffectRef> effect(colorFilter->asNewEffect(dev->context()));
- if (NULL != effect.get()) {
- grPaint->addColorEffect(effect);
- }
- }
- }
-
- return true;
-}
-
-// This function is similar to skPaint2GrPaintNoShader but also converts
-// skPaint's shader to a GrTexture/GrEffectStage if possible. The texture to
-// be used is set on grPaint and returned in param act. constantColor has the
-// same meaning as in skPaint2GrPaintNoShader.
-inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
- const SkPaint& skPaint,
- bool constantColor,
- GrPaint* grPaint) {
- SkShader* shader = skPaint.getShader();
- if (NULL == shader) {
- return skPaint2GrPaintNoShader(dev, skPaint, false, constantColor, grPaint);
- }
-
- // SkShader::asNewEffect() may do offscreen rendering. Setup default drawing state and require
- // the shader to set a render target .
- GrContext::AutoWideOpenIdentityDraw awo(dev->context(), NULL);
-
- // setup the shader as the first color effect on the paint
- SkAutoTUnref<GrEffectRef> effect(shader->asNewEffect(dev->context(), skPaint, NULL));
- if (NULL != effect.get()) {
- grPaint->addColorEffect(effect);
- // Now setup the rest of the paint.
- return skPaint2GrPaintNoShader(dev, skPaint, true, false, grPaint);
- } else {
- // We still don't have SkColorShader::asNewEffect() implemented.
- SkShader::GradientInfo info;
- SkColor color;
-
- info.fColors = &color;
- info.fColorOffsets = NULL;
- info.fColorCount = 1;
- if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
- SkPaint copy(skPaint);
- copy.setShader(NULL);
- // modulate the paint alpha by the shader's solid color alpha
- U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
- copy.setColor(SkColorSetA(color, newA));
- return skPaint2GrPaintNoShader(dev, copy, false, constantColor, grPaint);
- } else {
- return false;
- }
- }
-}
-}
-
///////////////////////////////////////////////////////////////////////////////
SkBitmap::Config SkGpuDevice::config() const {
CHECK_SHOULD_DRAW(draw, false);
GrPaint grPaint;
- if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
- return;
- }
+ SkPaint2GrPaintShader(this, paint, true, &grPaint);
fContext->drawPaint(grPaint);
}
}
GrPaint grPaint;
- if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
- return;
- }
+ SkPaint2GrPaintShader(this, paint, true, &grPaint);
fContext->drawVertices(grPaint,
gPointMode2PrimtiveType[mode],
}
GrPaint grPaint;
- if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
- return;
- }
+ SkPaint2GrPaintShader(this, paint, true, &grPaint);
if (!doStroke) {
fContext->drawRect(grPaint, rect);
CHECK_SHOULD_DRAW(draw, false);
GrPaint grPaint;
- if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
- return;
- }
+ SkPaint2GrPaintShader(this, paint, true, &grPaint);
SkStrokeRec stroke(paint);
if (paint.getMaskFilter()) {
CHECK_SHOULD_DRAW(draw, false);
GrPaint grPaint;
- if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
- return;
- }
+ SkPaint2GrPaintShader(this, paint, true, &grPaint);
if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
fContext->drawDRRect(grPaint, outer, inner);
}
GrPaint grPaint;
- if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
- return;
- }
+ SkPaint2GrPaintShader(this, paint, true, &grPaint);
SkStrokeRec stroke(paint);
fContext->drawOval(grPaint, oval, stroke);
CHECK_SHOULD_DRAW(draw, false);
GrPaint grPaint;
- if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
- return;
- }
+ SkPaint2GrPaintShader(this, paint, true, &grPaint);
// If we have a prematrix, apply it to the path, optimizing for the case
// where the original path can in fact be modified in place (even though
GrPaint grPaint;
grPaint.addColorEffect(effect);
bool alphaOnly = !(SkBitmap::kA8_Config == bitmap.config());
- if (!skPaint2GrPaintNoShader(this, paint, alphaOnly, false, &grPaint)) {
- return;
- }
+ SkPaint2GrPaintNoShader(this, paint, alphaOnly, false, &grPaint);
fContext->drawRectToRect(grPaint, dstRect, paintRect, NULL);
}
GrPaint grPaint;
grPaint.addColorTextureEffect(texture, SkMatrix::I());
- if(!skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
- return;
- }
+ SkPaint2GrPaintNoShader(this, paint, true, false, &grPaint);
fContext->drawRectToRect(grPaint,
SkRect::MakeXYWH(SkIntToScalar(left),
GrPaint grPaint;
grPaint.addColorTextureEffect(devTex, SkMatrix::I());
- if (!skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
- return;
- }
+ SkPaint2GrPaintNoShader(this, paint, true, false, &grPaint);
SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
SkIntToScalar(y),
GrPaint grPaint;
// we ignore the shader if texs is null.
if (NULL == texs) {
- if (!skPaint2GrPaintNoShader(this, paint, false, NULL == colors, &grPaint)) {
- return;
- }
+ SkPaint2GrPaintNoShader(this, paint, false, NULL == colors, &grPaint);
} else {
- if (!skPaint2GrPaintShader(this, paint, NULL == colors, &grPaint)) {
- return;
- }
+ SkPaint2GrPaintShader(this, paint, NULL == colors, &grPaint);
}
if (NULL != xmode && NULL != texs && NULL != colors) {
if (fMainTextContext->canDraw(paint)) {
GrPaint grPaint;
- if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
- return;
- }
+ SkPaint2GrPaintShader(this, paint, true, &grPaint);
SkDEBUGCODE(this->validate();)
fMainTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
} else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
GrPaint grPaint;
- if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
- return;
- }
+ SkPaint2GrPaintShader(this, paint, true, &grPaint);
SkDEBUGCODE(this->validate();)
if (fMainTextContext->canDraw(paint)) {
GrPaint grPaint;
- if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
- return;
- }
+ SkPaint2GrPaintShader(this, paint, true, &grPaint);
SkDEBUGCODE(this->validate();)
constY, scalarsPerPos);
} else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
GrPaint grPaint;
- if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
- return;
- }
+ SkPaint2GrPaintShader(this, paint, true, &grPaint);
SkDEBUGCODE(this->validate();)
*/
#include "SkGr.h"
+#include "SkColorFilter.h"
#include "SkConfig8888.h"
+#include "SkGpuDevice.h"
#include "SkMessageBus.h"
#include "SkPixelRef.h"
#include "GrResourceCache.h"
}
return true;
}
+
+///////////////////////////////////////////////////////////////////////////////
+
+void SkPaint2GrPaintNoShader(SkGpuDevice* dev, const SkPaint& skPaint, bool justAlpha,
+ bool constantColor, GrPaint* grPaint) {
+
+ grPaint->setDither(skPaint.isDither());
+ grPaint->setAntiAlias(skPaint.isAntiAlias());
+
+ SkXfermode::Coeff sm;
+ SkXfermode::Coeff dm;
+
+ SkXfermode* mode = skPaint.getXfermode();
+ GrEffectRef* xferEffect = NULL;
+ if (SkXfermode::AsNewEffectOrCoeff(mode, &xferEffect, &sm, &dm)) {
+ if (NULL != xferEffect) {
+ grPaint->addColorEffect(xferEffect)->unref();
+ sm = SkXfermode::kOne_Coeff;
+ dm = SkXfermode::kZero_Coeff;
+ }
+ } else {
+ //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
+ // Fall back to src-over
+ sm = SkXfermode::kOne_Coeff;
+ dm = SkXfermode::kISA_Coeff;
+ }
+ grPaint->setBlendFunc(sk_blend_to_grblend(sm), sk_blend_to_grblend(dm));
+
+ if (justAlpha) {
+ uint8_t alpha = skPaint.getAlpha();
+ grPaint->setColor(GrColorPackRGBA(alpha, alpha, alpha, alpha));
+ // justAlpha is currently set to true only if there is a texture,
+ // so constantColor should not also be true.
+ SkASSERT(!constantColor);
+ } else {
+ grPaint->setColor(SkColor2GrColor(skPaint.getColor()));
+ }
+
+ SkColorFilter* colorFilter = skPaint.getColorFilter();
+ if (NULL != colorFilter) {
+ // if the source color is a constant then apply the filter here once rather than per pixel
+ // in a shader.
+ if (constantColor) {
+ SkColor filtered = colorFilter->filterColor(skPaint.getColor());
+ grPaint->setColor(SkColor2GrColor(filtered));
+ } else {
+ SkAutoTUnref<GrEffectRef> effect(colorFilter->asNewEffect(dev->context()));
+ if (NULL != effect.get()) {
+ grPaint->addColorEffect(effect);
+ }
+ }
+ }
+}
+
+void SkPaint2GrPaintShader(SkGpuDevice* dev, const SkPaint& skPaint,
+ bool constantColor, GrPaint* grPaint) {
+ SkShader* shader = skPaint.getShader();
+ if (NULL == shader) {
+ SkPaint2GrPaintNoShader(dev, skPaint, false, constantColor, grPaint);
+ return;
+ }
+
+ // SkShader::asNewEffect() may do offscreen rendering. Setup default drawing state and require
+ // the shader to set a render target.
+ GrContext::AutoWideOpenIdentityDraw awo(dev->context(), NULL);
+
+ // setup the shader as the first color effect on the paint
+ SkAutoTUnref<GrEffectRef> effect(shader->asNewEffect(dev->context(), skPaint, NULL));
+ if (NULL != effect.get()) {
+ grPaint->addColorEffect(effect);
+ // Now setup the rest of the paint.
+ SkPaint2GrPaintNoShader(dev, skPaint, true, false, grPaint);
+ } else {
+ // We still don't have SkColorShader::asNewEffect() implemented.
+ SkShader::GradientInfo info;
+ SkColor color;
+
+ info.fColors = &color;
+ info.fColorOffsets = NULL;
+ info.fColorCount = 1;
+ if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
+ SkPaint copy(skPaint);
+ copy.setShader(NULL);
+ // modulate the paint alpha by the shader's solid color alpha
+ U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
+ copy.setColor(SkColorSetA(color, newA));
+ SkPaint2GrPaintNoShader(dev, copy, false, constantColor, grPaint);
+ } else {
+ SkPaint2GrPaintNoShader(dev, skPaint, false, constantColor, grPaint);
+ }
+ }
+}
+