LCOV - code coverage report
Current view: top level - gfx/skia/skia/src/core - SkShader.cpp (source / functions) Hit Total Coverage
Test: output.info Lines: 34 130 26.2 %
Date: 2017-07-14 16:53:18 Functions: 10 28 35.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright 2006 The Android Open Source Project
       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             : #include "SkArenaAlloc.h"
       9             : #include "SkAtomics.h"
      10             : #include "SkBitmapProcShader.h"
      11             : #include "SkColorShader.h"
      12             : #include "SkEmptyShader.h"
      13             : #include "SkMallocPixelRef.h"
      14             : #include "SkPaint.h"
      15             : #include "SkPicture.h"
      16             : #include "SkPictureShader.h"
      17             : #include "SkPM4fPriv.h"
      18             : #include "SkRasterPipeline.h"
      19             : #include "SkReadBuffer.h"
      20             : #include "SkScalar.h"
      21             : #include "SkShader.h"
      22             : #include "SkTLazy.h"
      23             : #include "SkWriteBuffer.h"
      24             : 
      25             : #if SK_SUPPORT_GPU
      26             : #include "GrFragmentProcessor.h"
      27             : #endif
      28             : 
      29             : //#define SK_TRACK_SHADER_LIFETIME
      30             : 
      31             : #ifdef SK_TRACK_SHADER_LIFETIME
      32             :     static int32_t gShaderCounter;
      33             : #endif
      34             : 
      35         171 : static inline void inc_shader_counter() {
      36             : #ifdef SK_TRACK_SHADER_LIFETIME
      37             :     int32_t prev = sk_atomic_inc(&gShaderCounter);
      38             :     SkDebugf("+++ shader counter %d\n", prev + 1);
      39             : #endif
      40         171 : }
      41         171 : static inline void dec_shader_counter() {
      42             : #ifdef SK_TRACK_SHADER_LIFETIME
      43             :     int32_t prev = sk_atomic_dec(&gShaderCounter);
      44             :     SkDebugf("--- shader counter %d\n", prev - 1);
      45             : #endif
      46         171 : }
      47             : 
      48         171 : SkShader::SkShader(const SkMatrix* localMatrix) {
      49         171 :     inc_shader_counter();
      50         171 :     if (localMatrix) {
      51         149 :         fLocalMatrix = *localMatrix;
      52             :     } else {
      53          22 :         fLocalMatrix.reset();
      54             :     }
      55             :     // Pre-cache so future calls to fLocalMatrix.getType() are threadsafe.
      56         171 :     (void)fLocalMatrix.getType();
      57         171 : }
      58             : 
      59         342 : SkShader::~SkShader() {
      60         171 :     dec_shader_counter();
      61         171 : }
      62             : 
      63           0 : void SkShader::flatten(SkWriteBuffer& buffer) const {
      64           0 :     this->INHERITED::flatten(buffer);
      65           0 :     bool hasLocalM = !fLocalMatrix.isIdentity();
      66           0 :     buffer.writeBool(hasLocalM);
      67           0 :     if (hasLocalM) {
      68           0 :         buffer.writeMatrix(fLocalMatrix);
      69             :     }
      70           0 : }
      71             : 
      72         473 : bool SkShader::computeTotalInverse(const ContextRec& rec, SkMatrix* totalInverse) const {
      73         473 :     SkMatrix total = SkMatrix::Concat(*rec.fMatrix, fLocalMatrix);
      74         473 :     if (rec.fLocalMatrix) {
      75           0 :         total.preConcat(*rec.fLocalMatrix);
      76             :     }
      77             : 
      78         473 :     return total.invert(totalInverse);
      79             : }
      80             : 
      81           0 : bool SkShader::asLuminanceColor(SkColor* colorPtr) const {
      82             :     SkColor storage;
      83           0 :     if (nullptr == colorPtr) {
      84           0 :         colorPtr = &storage;
      85             :     }
      86           0 :     if (this->onAsLuminanceColor(colorPtr)) {
      87           0 :         *colorPtr = SkColorSetA(*colorPtr, 0xFF);   // we only return opaque
      88           0 :         return true;
      89             :     }
      90           0 :     return false;
      91             : }
      92             : 
      93         166 : SkShader::Context* SkShader::makeContext(const ContextRec& rec, SkArenaAlloc* alloc) const {
      94         166 :     if (!this->computeTotalInverse(rec, nullptr)) {
      95           0 :         return nullptr;
      96             :     }
      97         166 :     return this->onMakeContext(rec, alloc);
      98             : }
      99             : 
     100         166 : SkShader::Context::Context(const SkShader& shader, const ContextRec& rec)
     101         166 :     : fShader(shader), fCTM(*rec.fMatrix)
     102             : {
     103             :     // Because the context parameters must be valid at this point, we know that the matrix is
     104             :     // invertible.
     105         166 :     SkAssertResult(fShader.computeTotalInverse(rec, &fTotalInverse));
     106         166 :     fTotalInverseClass = (uint8_t)ComputeMatrixClass(fTotalInverse);
     107             : 
     108         166 :     fPaintAlpha = rec.fPaint->getAlpha();
     109         166 : }
     110             : 
     111         166 : SkShader::Context::~Context() {}
     112             : 
     113           0 : SkShader::Context::ShadeProc SkShader::Context::asAShadeProc(void** ctx) {
     114           0 :     return nullptr;
     115             : }
     116             : 
     117           0 : void SkShader::Context::shadeSpan4f(int x, int y, SkPM4f dst[], int count) {
     118           0 :     const int N = 128;
     119             :     SkPMColor tmp[N];
     120           0 :     while (count > 0) {
     121           0 :         int n = SkTMin(count, N);
     122           0 :         this->shadeSpan(x, y, tmp, n);
     123           0 :         for (int i = 0; i < n; ++i) {
     124           0 :             dst[i] = SkPM4f::FromPMColor(tmp[i]);
     125             :         }
     126           0 :         dst += n;
     127           0 :         x += n;
     128           0 :         count -= n;
     129             :     }
     130           0 : }
     131             : 
     132             : #include "SkColorPriv.h"
     133             : 
     134             : #define kTempColorQuadCount 6   // balance between speed (larger) and saving stack-space
     135             : #define kTempColorCount     (kTempColorQuadCount << 2)
     136             : 
     137             : #ifdef SK_CPU_BENDIAN
     138             :     #define SkU32BitShiftToByteOffset(shift)    (3 - ((shift) >> 3))
     139             : #else
     140             :     #define SkU32BitShiftToByteOffset(shift)    ((shift) >> 3)
     141             : #endif
     142             : 
     143           0 : void SkShader::Context::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
     144           0 :     SkASSERT(count > 0);
     145             : 
     146             :     SkPMColor   colors[kTempColorCount];
     147             : 
     148           0 :     while ((count -= kTempColorCount) >= 0) {
     149           0 :         this->shadeSpan(x, y, colors, kTempColorCount);
     150           0 :         x += kTempColorCount;
     151             : 
     152           0 :         const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
     153           0 :         int quads = kTempColorQuadCount;
     154           0 :         do {
     155           0 :             U8CPU a0 = srcA[0];
     156           0 :             U8CPU a1 = srcA[4];
     157           0 :             U8CPU a2 = srcA[8];
     158           0 :             U8CPU a3 = srcA[12];
     159           0 :             srcA += 4*4;
     160           0 :             *alpha++ = SkToU8(a0);
     161           0 :             *alpha++ = SkToU8(a1);
     162           0 :             *alpha++ = SkToU8(a2);
     163           0 :             *alpha++ = SkToU8(a3);
     164             :         } while (--quads != 0);
     165             :     }
     166           0 :     SkASSERT(count < 0);
     167           0 :     SkASSERT(count + kTempColorCount >= 0);
     168           0 :     if (count += kTempColorCount) {
     169           0 :         this->shadeSpan(x, y, colors, count);
     170             : 
     171           0 :         const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
     172           0 :         do {
     173           0 :             *alpha++ = *srcA;
     174           0 :             srcA += 4;
     175             :         } while (--count != 0);
     176             :     }
     177             : #if 0
     178             :     do {
     179             :         int n = count;
     180             :         if (n > kTempColorCount)
     181             :             n = kTempColorCount;
     182             :         SkASSERT(n > 0);
     183             : 
     184             :         this->shadeSpan(x, y, colors, n);
     185             :         x += n;
     186             :         count -= n;
     187             : 
     188             :         const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
     189             :         do {
     190             :             *alpha++ = *srcA;
     191             :             srcA += 4;
     192             :         } while (--n != 0);
     193             :     } while (count > 0);
     194             : #endif
     195           0 : }
     196             : 
     197         191 : SkShader::Context::MatrixClass SkShader::Context::ComputeMatrixClass(const SkMatrix& mat) {
     198         191 :     MatrixClass mc = kLinear_MatrixClass;
     199             : 
     200         191 :     if (mat.hasPerspective()) {
     201           0 :         if (mat.isFixedStepInX()) {
     202           0 :             mc = kFixedStepInX_MatrixClass;
     203             :         } else {
     204           0 :             mc = kPerspective_MatrixClass;
     205             :         }
     206             :     }
     207         191 :     return mc;
     208             : }
     209             : 
     210             : //////////////////////////////////////////////////////////////////////////////
     211             : 
     212           0 : SkShader::GradientType SkShader::asAGradient(GradientInfo* info) const {
     213           0 :     return kNone_GradientType;
     214             : }
     215             : 
     216             : #if SK_SUPPORT_GPU
     217           0 : sk_sp<GrFragmentProcessor> SkShader::asFragmentProcessor(const AsFPArgs&) const {
     218           0 :     return nullptr;
     219             : }
     220             : #endif
     221             : 
     222           0 : sk_sp<SkShader> SkShader::makeAsALocalMatrixShader(SkMatrix*) const {
     223           0 :     return nullptr;
     224             : }
     225             : 
     226           0 : sk_sp<SkShader> SkShader::MakeEmptyShader() { return sk_make_sp<SkEmptyShader>(); }
     227             : 
     228           0 : sk_sp<SkShader> SkShader::MakeColorShader(SkColor color) { return sk_make_sp<SkColorShader>(color); }
     229             : 
     230           0 : sk_sp<SkShader> SkShader::MakeBitmapShader(const SkBitmap& src, TileMode tmx, TileMode tmy,
     231             :                                            const SkMatrix* localMatrix) {
     232           0 :     if (localMatrix && !localMatrix->invert(nullptr)) {
     233           0 :         return nullptr;
     234             :     }
     235           0 :     return SkMakeBitmapShader(src, tmx, tmy, localMatrix, kIfMutable_SkCopyPixelsMode);
     236             : }
     237             : 
     238           0 : sk_sp<SkShader> SkShader::MakePictureShader(sk_sp<SkPicture> src, TileMode tmx, TileMode tmy,
     239             :                                             const SkMatrix* localMatrix, const SkRect* tile) {
     240           0 :     if (localMatrix && !localMatrix->invert(nullptr)) {
     241           0 :         return nullptr;
     242             :     }
     243           0 :     return SkPictureShader::Make(std::move(src), tmx, tmy, localMatrix, tile);
     244             : }
     245             : 
     246             : #ifndef SK_IGNORE_TO_STRING
     247           0 : void SkShader::toString(SkString* str) const {
     248           0 :     if (!fLocalMatrix.isIdentity()) {
     249           0 :         str->append(" ");
     250           0 :         fLocalMatrix.toString(str);
     251             :     }
     252           0 : }
     253             : #endif
     254             : 
     255           2 : bool SkShader::appendStages(SkRasterPipeline* pipeline,
     256             :                             SkColorSpace* dst,
     257             :                             SkArenaAlloc* scratch,
     258             :                             const SkMatrix& ctm,
     259             :                             const SkPaint& paint) const {
     260           2 :     return this->onAppendStages(pipeline, dst, scratch, ctm, paint, nullptr);
     261             : }
     262             : 
     263           0 : bool SkShader::onAppendStages(SkRasterPipeline* p,
     264             :                               SkColorSpace* cs,
     265             :                               SkArenaAlloc* alloc,
     266             :                               const SkMatrix& ctm,
     267             :                               const SkPaint& paint,
     268             :                               const SkMatrix* localM) const {
     269             :     // Legacy shaders handle the paint opacity internally,
     270             :     // but RP applies it as a separate stage.
     271           0 :     SkTCopyOnFirstWrite<SkPaint> opaquePaint(paint);
     272           0 :     if (paint.getAlpha() != SK_AlphaOPAQUE) {
     273           0 :         opaquePaint.writable()->setAlpha(SK_AlphaOPAQUE);
     274             :     }
     275             : 
     276           0 :     ContextRec rec(*opaquePaint, ctm, localM, ContextRec::kPM4f_DstType, cs);
     277           0 :     if (auto* ctx = this->makeContext(rec, alloc)) {
     278           0 :         p->append(SkRasterPipeline::shader_adapter, ctx);
     279             : 
     280             :         // Legacy shaders aren't aware of color spaces. We can pretty
     281             :         // safely assume they're in sRGB gamut.
     282           0 :         return append_gamut_transform(p, alloc,
     283           0 :                                       SkColorSpace::MakeSRGB().get(), cs);
     284             :     }
     285           0 :     return false;
     286             : }
     287             : 
     288             : ///////////////////////////////////////////////////////////////////////////////////////////////////
     289             : 
     290           0 : sk_sp<SkFlattenable> SkEmptyShader::CreateProc(SkReadBuffer&) {
     291           0 :     return SkShader::MakeEmptyShader();
     292             : }
     293             : 
     294             : #ifndef SK_IGNORE_TO_STRING
     295             : #include "SkEmptyShader.h"
     296             : 
     297           0 : void SkEmptyShader::toString(SkString* str) const {
     298           0 :     str->append("SkEmptyShader: (");
     299             : 
     300           0 :     this->INHERITED::toString(str);
     301             : 
     302           0 :     str->append(")");
     303           0 : }
     304             : #endif

Generated by: LCOV version 1.13