Home | History | Annotate | Download | only in gpu
      1 /*
      2  * Copyright 2010 Google Inc.
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 
      9 #include "SkGr.h"
     10 
     11 #include "GrCaps.h"
     12 #include "GrContext.h"
     13 #include "GrTextureParamsAdjuster.h"
     14 #include "GrGpuResourcePriv.h"
     15 #include "GrImageIDTextureAdjuster.h"
     16 #include "GrXferProcessor.h"
     17 #include "GrYUVProvider.h"
     18 
     19 #include "SkColorFilter.h"
     20 #include "SkConfig8888.h"
     21 #include "SkCanvas.h"
     22 #include "SkData.h"
     23 #include "SkErrorInternals.h"
     24 #include "SkGrPixelRef.h"
     25 #include "SkMessageBus.h"
     26 #include "SkPixelRef.h"
     27 #include "SkResourceCache.h"
     28 #include "SkTextureCompressor.h"
     29 #include "SkYUVPlanesCache.h"
     30 #include "effects/GrBicubicEffect.h"
     31 #include "effects/GrConstColorProcessor.h"
     32 #include "effects/GrDitherEffect.h"
     33 #include "effects/GrPorterDuffXferProcessor.h"
     34 #include "effects/GrXfermodeFragmentProcessor.h"
     35 #include "effects/GrYUVEffect.h"
     36 
     37 #ifndef SK_IGNORE_ETC1_SUPPORT
     38 #  include "ktx.h"
     39 #  include "etc1.h"
     40 #endif
     41 
     42 GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo& info) {
     43     GrSurfaceDesc desc;
     44     desc.fFlags = kNone_GrSurfaceFlags;
     45     desc.fWidth = info.width();
     46     desc.fHeight = info.height();
     47     desc.fConfig = SkImageInfo2GrPixelConfig(info);
     48     desc.fSampleCnt = 0;
     49     return desc;
     50 }
     51 
     52 void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds) {
     53     SkASSERT(key);
     54     SkASSERT(imageID);
     55     SkASSERT(!imageBounds.isEmpty());
     56     static const GrUniqueKey::Domain kImageIDDomain = GrUniqueKey::GenerateDomain();
     57     GrUniqueKey::Builder builder(key, kImageIDDomain, 5);
     58     builder[0] = imageID;
     59     builder[1] = imageBounds.fLeft;
     60     builder[2] = imageBounds.fTop;
     61     builder[3] = imageBounds.fRight;
     62     builder[4] = imageBounds.fBottom;
     63 }
     64 
     65 GrPixelConfig GrIsCompressedTextureDataSupported(GrContext* ctx, SkData* data,
     66                                                  int expectedW, int expectedH,
     67                                                  const void** outStartOfDataToUpload) {
     68     *outStartOfDataToUpload = nullptr;
     69 #ifndef SK_IGNORE_ETC1_SUPPORT
     70     if (!ctx->caps()->isConfigTexturable(kETC1_GrPixelConfig)) {
     71         return kUnknown_GrPixelConfig;
     72     }
     73 
     74     const uint8_t* bytes = data->bytes();
     75     if (data->size() > ETC_PKM_HEADER_SIZE && etc1_pkm_is_valid(bytes)) {
     76         // Does the data match the dimensions of the bitmap? If not,
     77         // then we don't know how to scale the image to match it...
     78         if (etc1_pkm_get_width(bytes) != (unsigned)expectedW ||
     79             etc1_pkm_get_height(bytes) != (unsigned)expectedH)
     80         {
     81             return kUnknown_GrPixelConfig;
     82         }
     83 
     84         *outStartOfDataToUpload = bytes + ETC_PKM_HEADER_SIZE;
     85         return kETC1_GrPixelConfig;
     86     } else if (SkKTXFile::is_ktx(bytes)) {
     87         SkKTXFile ktx(data);
     88 
     89         // Is it actually an ETC1 texture?
     90         if (!ktx.isCompressedFormat(SkTextureCompressor::kETC1_Format)) {
     91             return kUnknown_GrPixelConfig;
     92         }
     93 
     94         // Does the data match the dimensions of the bitmap? If not,
     95         // then we don't know how to scale the image to match it...
     96         if (ktx.width() != expectedW || ktx.height() != expectedH) {
     97             return kUnknown_GrPixelConfig;
     98         }
     99 
    100         *outStartOfDataToUpload = ktx.pixelData();
    101         return kETC1_GrPixelConfig;
    102     }
    103 #endif
    104     return kUnknown_GrPixelConfig;
    105 }
    106 
    107 //////////////////////////////////////////////////////////////////////////////
    108 
    109 /**
    110  * Fill out buffer with the compressed format Ganesh expects from a colortable
    111  * based bitmap. [palette (colortable) + indices].
    112  *
    113  * At the moment Ganesh only supports 8bit version. If Ganesh allowed we others
    114  * we could detect that the colortable.count is <= 16, and then repack the
    115  * indices as nibbles to save RAM, but it would take more time (i.e. a lot
    116  * slower than memcpy), so skipping that for now.
    117  *
    118  * Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big
    119  * as the colortable.count says it is.
    120  */
    121 static void build_index8_data(void* buffer, const SkBitmap& bitmap) {
    122     SkASSERT(kIndex_8_SkColorType == bitmap.colorType());
    123 
    124     SkAutoLockPixels alp(bitmap);
    125     if (!bitmap.readyToDraw()) {
    126         SkDEBUGFAIL("bitmap not ready to draw!");
    127         return;
    128     }
    129 
    130     SkColorTable* ctable = bitmap.getColorTable();
    131     char* dst = (char*)buffer;
    132 
    133     const int count = ctable->count();
    134 
    135     SkDstPixelInfo dstPI;
    136     dstPI.fColorType = kRGBA_8888_SkColorType;
    137     dstPI.fAlphaType = kPremul_SkAlphaType;
    138     dstPI.fPixels = buffer;
    139     dstPI.fRowBytes = count * sizeof(SkPMColor);
    140 
    141     SkSrcPixelInfo srcPI;
    142     srcPI.fColorType = kN32_SkColorType;
    143     srcPI.fAlphaType = kPremul_SkAlphaType;
    144     srcPI.fPixels = ctable->readColors();
    145     srcPI.fRowBytes = count * sizeof(SkPMColor);
    146 
    147     srcPI.convertPixelsTo(&dstPI, count, 1);
    148 
    149     // always skip a full 256 number of entries, even if we memcpy'd fewer
    150     dst += 256 * sizeof(GrColor);
    151 
    152     if ((unsigned)bitmap.width() == bitmap.rowBytes()) {
    153         memcpy(dst, bitmap.getPixels(), bitmap.getSize());
    154     } else {
    155         // need to trim off the extra bytes per row
    156         size_t width = bitmap.width();
    157         size_t rowBytes = bitmap.rowBytes();
    158         const char* src = (const char*)bitmap.getPixels();
    159         for (int y = 0; y < bitmap.height(); y++) {
    160             memcpy(dst, src, width);
    161             src += rowBytes;
    162             dst += width;
    163         }
    164     }
    165 }
    166 
    167 /**
    168  *  Once we have made SkImages handle all lazy/deferred/generated content, the YUV apis will
    169  *  be gone from SkPixelRef, and we can remove this subclass entirely.
    170  */
    171 class PixelRef_GrYUVProvider : public GrYUVProvider {
    172     SkPixelRef* fPR;
    173 
    174 public:
    175     PixelRef_GrYUVProvider(SkPixelRef* pr) : fPR(pr) {}
    176 
    177     uint32_t onGetID() override { return fPR->getGenerationID(); }
    178     bool onGetYUVSizes(SkISize sizes[3]) override {
    179         return fPR->getYUV8Planes(sizes, nullptr, nullptr, nullptr);
    180     }
    181     bool onGetYUVPlanes(SkISize sizes[3], void* planes[3], size_t rowBytes[3],
    182                         SkYUVColorSpace* space) override {
    183         return fPR->getYUV8Planes(sizes, planes, rowBytes, space);
    184     }
    185 };
    186 
    187 static GrTexture* create_texture_from_yuv(GrContext* ctx, const SkBitmap& bm,
    188                                           const GrSurfaceDesc& desc) {
    189     // Subsets are not supported, the whole pixelRef is loaded when using YUV decoding
    190     SkPixelRef* pixelRef = bm.pixelRef();
    191     if ((nullptr == pixelRef) ||
    192         (pixelRef->info().width() != bm.info().width()) ||
    193         (pixelRef->info().height() != bm.info().height())) {
    194         return nullptr;
    195     }
    196 
    197     PixelRef_GrYUVProvider provider(pixelRef);
    198 
    199     return provider.refAsTexture(ctx, desc, !bm.isVolatile());
    200 }
    201 
    202 static GrTexture* load_etc1_texture(GrContext* ctx, const SkBitmap &bm, GrSurfaceDesc desc) {
    203     SkAutoTUnref<SkData> data(bm.pixelRef()->refEncodedData());
    204     if (!data) {
    205         return nullptr;
    206     }
    207 
    208     const void* startOfTexData;
    209     desc.fConfig = GrIsCompressedTextureDataSupported(ctx, data, bm.width(), bm.height(),
    210                                                       &startOfTexData);
    211     if (kUnknown_GrPixelConfig == desc.fConfig) {
    212         return nullptr;
    213     }
    214 
    215     return ctx->textureProvider()->createTexture(desc, SkBudgeted::kYes, startOfTexData, 0);
    216 }
    217 
    218 GrTexture* GrUploadBitmapToTexture(GrContext* ctx, const SkBitmap& bmp) {
    219     SkASSERT(!bmp.getTexture());
    220 
    221     SkBitmap tmpBitmap;
    222     const SkBitmap* bitmap = &bmp;
    223 
    224     GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap->info());
    225     const GrCaps* caps = ctx->caps();
    226 
    227     if (kIndex_8_SkColorType == bitmap->colorType()) {
    228         if (caps->isConfigTexturable(kIndex_8_GrPixelConfig)) {
    229             size_t imageSize = GrCompressedFormatDataSize(kIndex_8_GrPixelConfig,
    230                                                           bitmap->width(), bitmap->height());
    231             SkAutoMalloc storage(imageSize);
    232             build_index8_data(storage.get(), bmp);
    233 
    234             // our compressed data will be trimmed, so pass width() for its
    235             // "rowBytes", since they are the same now.
    236             return ctx->textureProvider()->createTexture(desc, SkBudgeted::kYes, storage.get(),
    237                                                          bitmap->width());
    238         } else {
    239             bmp.copyTo(&tmpBitmap, kN32_SkColorType);
    240             // now bitmap points to our temp, which has been promoted to 32bits
    241             bitmap = &tmpBitmap;
    242             desc.fConfig = SkImageInfo2GrPixelConfig(bitmap->info());
    243         }
    244     } else if (!bitmap->readyToDraw()) {
    245         // If the bitmap had compressed data and was then uncompressed, it'll still return
    246         // compressed data on 'refEncodedData' and upload it. Probably not good, since if
    247         // the bitmap has available pixels, then they might not be what the decompressed
    248         // data is.
    249 
    250         // Really?? We aren't doing this with YUV.
    251 
    252         GrTexture *texture = load_etc1_texture(ctx, *bitmap, desc);
    253         if (texture) {
    254             return texture;
    255         }
    256     }
    257 
    258     GrTexture *texture = create_texture_from_yuv(ctx, *bitmap, desc);
    259     if (texture) {
    260         return texture;
    261     }
    262 
    263     SkAutoLockPixels alp(*bitmap);
    264     if (!bitmap->readyToDraw()) {
    265         return nullptr;
    266     }
    267 
    268     return ctx->textureProvider()->createTexture(desc, SkBudgeted::kYes, bitmap->getPixels(),
    269                                                  bitmap->rowBytes());
    270 }
    271 
    272 
    273 ////////////////////////////////////////////////////////////////////////////////
    274 
    275 void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, SkPixelRef* pixelRef) {
    276     class Invalidator : public SkPixelRef::GenIDChangeListener {
    277     public:
    278         explicit Invalidator(const GrUniqueKey& key) : fMsg(key) {}
    279     private:
    280         GrUniqueKeyInvalidatedMessage fMsg;
    281 
    282         void onChange() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); }
    283     };
    284 
    285     pixelRef->addGenIDChangeListener(new Invalidator(key));
    286 }
    287 
    288 GrTexture* GrRefCachedBitmapTexture(GrContext* ctx, const SkBitmap& bitmap,
    289                                     const GrTextureParams& params) {
    290     if (bitmap.getTexture()) {
    291         return GrBitmapTextureAdjuster(&bitmap).refTextureSafeForParams(params, nullptr);
    292     }
    293     return GrBitmapTextureMaker(ctx, bitmap).refTextureForParams(params);
    294 }
    295 
    296 ///////////////////////////////////////////////////////////////////////////////
    297 
    298 // alphatype is ignore for now, but if GrPixelConfig is expanded to encompass
    299 // alpha info, that will be considered.
    300 GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, SkColorProfileType pt) {
    301     switch (ct) {
    302         case kUnknown_SkColorType:
    303             return kUnknown_GrPixelConfig;
    304         case kAlpha_8_SkColorType:
    305             return kAlpha_8_GrPixelConfig;
    306         case kRGB_565_SkColorType:
    307             return kRGB_565_GrPixelConfig;
    308         case kARGB_4444_SkColorType:
    309             return kRGBA_4444_GrPixelConfig;
    310         case kRGBA_8888_SkColorType:
    311             //if (kSRGB_SkColorProfileType == pt) {
    312             //    return kSRGBA_8888_GrPixelConfig;
    313             //}
    314             return kRGBA_8888_GrPixelConfig;
    315         case kBGRA_8888_SkColorType:
    316             return kBGRA_8888_GrPixelConfig;
    317         case kIndex_8_SkColorType:
    318             return kIndex_8_GrPixelConfig;
    319         case kGray_8_SkColorType:
    320             return kAlpha_8_GrPixelConfig; // TODO: gray8 support on gpu
    321         case kRGBA_F16_SkColorType:
    322             return kRGBA_half_GrPixelConfig;
    323     }
    324     SkASSERT(0);    // shouldn't get here
    325     return kUnknown_GrPixelConfig;
    326 }
    327 
    328 bool GrPixelConfig2ColorAndProfileType(GrPixelConfig config, SkColorType* ctOut,
    329                                        SkColorProfileType* ptOut) {
    330     SkColorType ct;
    331     SkColorProfileType pt = kLinear_SkColorProfileType;
    332     switch (config) {
    333         case kAlpha_8_GrPixelConfig:
    334             ct = kAlpha_8_SkColorType;
    335             break;
    336         case kIndex_8_GrPixelConfig:
    337             ct = kIndex_8_SkColorType;
    338             break;
    339         case kRGB_565_GrPixelConfig:
    340             ct = kRGB_565_SkColorType;
    341             break;
    342         case kRGBA_4444_GrPixelConfig:
    343             ct = kARGB_4444_SkColorType;
    344             break;
    345         case kRGBA_8888_GrPixelConfig:
    346             ct = kRGBA_8888_SkColorType;
    347             break;
    348         case kBGRA_8888_GrPixelConfig:
    349             ct = kBGRA_8888_SkColorType;
    350             break;
    351         case kSRGBA_8888_GrPixelConfig:
    352             ct = kRGBA_8888_SkColorType;
    353             pt = kSRGB_SkColorProfileType;
    354             break;
    355         default:
    356             return false;
    357     }
    358     if (ctOut) {
    359         *ctOut = ct;
    360     }
    361     if (ptOut) {
    362         *ptOut = pt;
    363     }
    364     return true;
    365 }
    366 
    367 ////////////////////////////////////////////////////////////////////////////////////////////////
    368 
    369 static inline bool blend_requires_shader(const SkXfermode::Mode mode, bool primitiveIsSrc) {
    370     if (primitiveIsSrc) {
    371         return SkXfermode::kSrc_Mode != mode;
    372     } else {
    373         return SkXfermode::kDst_Mode != mode;
    374     }
    375 }
    376 
    377 static inline bool skpaint_to_grpaint_impl(GrContext* context,
    378                                            const SkPaint& skPaint,
    379                                            const SkMatrix& viewM,
    380                                            const GrFragmentProcessor** shaderProcessor,
    381                                            SkXfermode::Mode* primColorMode,
    382                                            bool primitiveIsSrc,
    383                                            GrPaint* grPaint) {
    384     grPaint->setAntiAlias(skPaint.isAntiAlias());
    385 
    386     // Setup the initial color considering the shader, the SkPaint color, and the presence or not
    387     // of per-vertex colors.
    388     SkAutoTUnref<const GrFragmentProcessor> aufp;
    389     const GrFragmentProcessor* shaderFP = nullptr;
    390     if (!primColorMode || blend_requires_shader(*primColorMode, primitiveIsSrc)) {
    391         if (shaderProcessor) {
    392             shaderFP = *shaderProcessor;
    393         } else if (const SkShader* shader = skPaint.getShader()) {
    394             aufp.reset(shader->asFragmentProcessor(context, viewM, nullptr,
    395                                                    skPaint.getFilterQuality()));
    396             shaderFP = aufp;
    397             if (!shaderFP) {
    398                 return false;
    399             }
    400         }
    401     }
    402 
    403     // Set this in below cases if the output of the shader/paint-color/paint-alpha/primXfermode is
    404     // a known constant value. In that case we can simply apply a color filter during this
    405     // conversion without converting the color filter to a GrFragmentProcessor.
    406     bool applyColorFilterToPaintColor = false;
    407     if (shaderFP) {
    408         if (primColorMode) {
    409             // There is a blend between the primitive color and the shader color. The shader sees
    410             // the opaque paint color. The shader's output is blended using the provided mode by
    411             // the primitive color. The blended color is then modulated by the paint's alpha.
    412 
    413             // The geometry processor will insert the primitive color to start the color chain, so
    414             // the GrPaint color will be ignored.
    415 
    416             GrColor shaderInput = SkColorToOpaqueGrColor(skPaint.getColor());
    417 
    418             shaderFP = GrFragmentProcessor::OverrideInput(shaderFP, shaderInput);
    419             aufp.reset(shaderFP);
    420 
    421             if (primitiveIsSrc) {
    422                 shaderFP = GrXfermodeFragmentProcessor::CreateFromDstProcessor(shaderFP,
    423                                                                                *primColorMode);
    424             } else {
    425                 shaderFP = GrXfermodeFragmentProcessor::CreateFromSrcProcessor(shaderFP,
    426                                                                                *primColorMode);
    427             }
    428             aufp.reset(shaderFP);
    429             // The above may return null if compose results in a pass through of the prim color.
    430             if (shaderFP) {
    431                 grPaint->addColorFragmentProcessor(shaderFP);
    432             }
    433 
    434             GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor());
    435             if (GrColor_WHITE != paintAlpha) {
    436                 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Create(
    437                     paintAlpha, GrConstColorProcessor::kModulateRGBA_InputMode))->unref();
    438             }
    439         } else {
    440             // The shader's FP sees the paint unpremul color
    441             grPaint->setColor(SkColorToUnpremulGrColor(skPaint.getColor()));
    442             grPaint->addColorFragmentProcessor(shaderFP);
    443         }
    444     } else {
    445         if (primColorMode) {
    446             // There is a blend between the primitive color and the paint color. The blend considers
    447             // the opaque paint color. The paint's alpha is applied to the post-blended color.
    448             SkAutoTUnref<const GrFragmentProcessor> processor(
    449                 GrConstColorProcessor::Create(SkColorToOpaqueGrColor(skPaint.getColor()),
    450                                               GrConstColorProcessor::kIgnore_InputMode));
    451             if (primitiveIsSrc) {
    452                 processor.reset(GrXfermodeFragmentProcessor::CreateFromDstProcessor(processor,
    453                                                                                 *primColorMode));
    454             } else {
    455                 processor.reset(GrXfermodeFragmentProcessor::CreateFromSrcProcessor(processor,
    456                                                                                 *primColorMode));
    457 
    458             }
    459             if (processor) {
    460                 grPaint->addColorFragmentProcessor(processor);
    461             }
    462 
    463             grPaint->setColor(SkColorToOpaqueGrColor(skPaint.getColor()));
    464 
    465             GrColor paintAlpha = SkColorAlphaToGrColor(skPaint.getColor());
    466             if (GrColor_WHITE != paintAlpha) {
    467                 grPaint->addColorFragmentProcessor(GrConstColorProcessor::Create(
    468                     paintAlpha, GrConstColorProcessor::kModulateRGBA_InputMode))->unref();
    469             }
    470         } else {
    471             // No shader, no primitive color.
    472             grPaint->setColor(SkColorToPremulGrColor(skPaint.getColor()));
    473             applyColorFilterToPaintColor = true;
    474         }
    475     }
    476 
    477     SkColorFilter* colorFilter = skPaint.getColorFilter();
    478     if (colorFilter) {
    479         if (applyColorFilterToPaintColor) {
    480             grPaint->setColor(SkColorToPremulGrColor(colorFilter->filterColor(skPaint.getColor())));
    481         } else {
    482             SkAutoTUnref<const GrFragmentProcessor> cfFP(
    483                 colorFilter->asFragmentProcessor(context));
    484             if (cfFP) {
    485                 grPaint->addColorFragmentProcessor(cfFP);
    486             } else {
    487                 return false;
    488             }
    489         }
    490     }
    491 
    492     // When the xfermode is null on the SkPaint (meaning kSrcOver) we need the XPFactory field on
    493     // the GrPaint to also be null (also kSrcOver).
    494     SkASSERT(!grPaint->getXPFactory());
    495     SkXfermode* xfermode = skPaint.getXfermode();
    496     if (xfermode) {
    497         // SafeUnref in case a new xfermode is added that returns null.
    498         // In such cases we will fall back to kSrcOver_Mode.
    499         SkSafeUnref(grPaint->setXPFactory(xfermode->asXPFactory()));
    500     }
    501 
    502 #ifndef SK_IGNORE_GPU_DITHER
    503     if (skPaint.isDither() && grPaint->numColorFragmentProcessors() > 0) {
    504         grPaint->addColorFragmentProcessor(GrDitherEffect::Create())->unref();
    505     }
    506 #endif
    507     return true;
    508 }
    509 
    510 bool SkPaintToGrPaint(GrContext* context, const SkPaint& skPaint, const SkMatrix& viewM,
    511                       GrPaint* grPaint) {
    512     return skpaint_to_grpaint_impl(context, skPaint, viewM, nullptr, nullptr, false, grPaint);
    513 }
    514 
    515 /** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. */
    516 bool SkPaintToGrPaintReplaceShader(GrContext* context,
    517                                    const SkPaint& skPaint,
    518                                    const GrFragmentProcessor* shaderFP,
    519                                    GrPaint* grPaint) {
    520     if (!shaderFP) {
    521         return false;
    522     }
    523     return skpaint_to_grpaint_impl(context, skPaint, SkMatrix::I(), &shaderFP, nullptr, false,
    524                                    grPaint);
    525 }
    526 
    527 /** Ignores the SkShader (if any) on skPaint. */
    528 bool SkPaintToGrPaintNoShader(GrContext* context,
    529                               const SkPaint& skPaint,
    530                               GrPaint* grPaint) {
    531     // Use a ptr to a nullptr to to indicate that the SkShader is ignored and not replaced.
    532     static const GrFragmentProcessor* kNullShaderFP = nullptr;
    533     static const GrFragmentProcessor** kIgnoreShader = &kNullShaderFP;
    534     return skpaint_to_grpaint_impl(context, skPaint, SkMatrix::I(), kIgnoreShader, nullptr, false,
    535                                    grPaint);
    536 }
    537 
    538 /** Blends the SkPaint's shader (or color if no shader) with a per-primitive color which must
    539 be setup as a vertex attribute using the specified SkXfermode::Mode. */
    540 bool SkPaintToGrPaintWithXfermode(GrContext* context,
    541                                   const SkPaint& skPaint,
    542                                   const SkMatrix& viewM,
    543                                   SkXfermode::Mode primColorMode,
    544                                   bool primitiveIsSrc,
    545                                   GrPaint* grPaint) {
    546     return skpaint_to_grpaint_impl(context, skPaint, viewM, nullptr, &primColorMode, primitiveIsSrc,
    547                                    grPaint);
    548 }
    549 
    550 bool SkPaintToGrPaintWithTexture(GrContext* context,
    551                                  const SkPaint& paint,
    552                                  const SkMatrix& viewM,
    553                                  const GrFragmentProcessor* fp,
    554                                  bool textureIsAlphaOnly,
    555                                  GrPaint* grPaint) {
    556     SkAutoTUnref<const GrFragmentProcessor> shaderFP;
    557     if (textureIsAlphaOnly) {
    558         if (const SkShader* shader = paint.getShader()) {
    559             shaderFP.reset(shader->asFragmentProcessor(context,
    560                                                        viewM,
    561                                                        nullptr,
    562                                                        paint.getFilterQuality()));
    563             if (!shaderFP) {
    564                 return false;
    565             }
    566             const GrFragmentProcessor* fpSeries[] = { shaderFP.get(), fp };
    567             shaderFP.reset(GrFragmentProcessor::RunInSeries(fpSeries, 2));
    568         } else {
    569             shaderFP.reset(GrFragmentProcessor::MulOutputByInputUnpremulColor(fp));
    570         }
    571     } else {
    572         shaderFP.reset(GrFragmentProcessor::MulOutputByInputAlpha(fp));
    573     }
    574 
    575     return SkPaintToGrPaintReplaceShader(context, paint, shaderFP.get(), grPaint);
    576 }
    577 
    578 
    579 ////////////////////////////////////////////////////////////////////////////////////////////////
    580 
    581 SkImageInfo GrMakeInfoFromTexture(GrTexture* tex, int w, int h, bool isOpaque) {
    582 #ifdef SK_DEBUG
    583     const GrSurfaceDesc& desc = tex->desc();
    584     SkASSERT(w <= desc.fWidth);
    585     SkASSERT(h <= desc.fHeight);
    586 #endif
    587     const GrPixelConfig config = tex->config();
    588     SkColorType ct;
    589     SkAlphaType at = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
    590     if (!GrPixelConfig2ColorAndProfileType(config, &ct, nullptr)) {
    591         ct = kUnknown_SkColorType;
    592     }
    593     return SkImageInfo::Make(w, h, ct, at);
    594 }
    595 
    596 
    597 void GrWrapTextureInBitmap(GrTexture* src, int w, int h, bool isOpaque, SkBitmap* dst) {
    598     const SkImageInfo info = GrMakeInfoFromTexture(src, w, h, isOpaque);
    599     dst->setInfo(info);
    600     dst->setPixelRef(new SkGrPixelRef(info, src))->unref();
    601 }
    602 
    603 GrTextureParams::FilterMode GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality,
    604                                                             const SkMatrix& viewM,
    605                                                             const SkMatrix& localM,
    606                                                             bool* doBicubic) {
    607     *doBicubic = false;
    608     GrTextureParams::FilterMode textureFilterMode;
    609     switch (paintFilterQuality) {
    610         case kNone_SkFilterQuality:
    611             textureFilterMode = GrTextureParams::kNone_FilterMode;
    612             break;
    613         case kLow_SkFilterQuality:
    614             textureFilterMode = GrTextureParams::kBilerp_FilterMode;
    615             break;
    616         case kMedium_SkFilterQuality: {
    617             SkMatrix matrix;
    618             matrix.setConcat(viewM, localM);
    619             if (matrix.getMinScale() < SK_Scalar1) {
    620                 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
    621             } else {
    622                 // Don't trigger MIP level generation unnecessarily.
    623                 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
    624             }
    625             break;
    626         }
    627         case kHigh_SkFilterQuality: {
    628             SkMatrix matrix;
    629             matrix.setConcat(viewM, localM);
    630             *doBicubic = GrBicubicEffect::ShouldUseBicubic(matrix, &textureFilterMode);
    631             break;
    632         }
    633         default:
    634             SkErrorInternals::SetError( kInvalidPaint_SkError,
    635                                         "Sorry, I don't understand the filtering "
    636                                         "mode you asked for.  Falling back to "
    637                                         "MIPMaps.");
    638             textureFilterMode = GrTextureParams::kMipMap_FilterMode;
    639             break;
    640 
    641     }
    642     return textureFilterMode;
    643 }
    644