Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright 2015 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 #include "SkColorSpaceXformer.h"
      9 #include "SkImageFilterPriv.h"
     10 #include "SkLocalMatrixImageFilter.h"
     11 #include "SkReadBuffer.h"
     12 #include "SkSpecialImage.h"
     13 #include "SkString.h"
     14 
     15 sk_sp<SkImageFilter> SkLocalMatrixImageFilter::Make(const SkMatrix& localM,
     16                                                     sk_sp<SkImageFilter> input) {
     17     if (!input) {
     18         return nullptr;
     19     }
     20     if (localM.getType() & (SkMatrix::kAffine_Mask | SkMatrix::kPerspective_Mask)) {
     21         return nullptr;
     22     }
     23     if (localM.isIdentity()) {
     24         return input;
     25     }
     26     return sk_sp<SkImageFilter>(new SkLocalMatrixImageFilter(localM, input));
     27 }
     28 
     29 SkLocalMatrixImageFilter::SkLocalMatrixImageFilter(const SkMatrix& localM,
     30                                                    sk_sp<SkImageFilter> input)
     31     : INHERITED(&input, 1, nullptr)
     32     , fLocalM(localM) {
     33 }
     34 
     35 sk_sp<SkFlattenable> SkLocalMatrixImageFilter::CreateProc(SkReadBuffer& buffer) {
     36     SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
     37     SkMatrix lm;
     38     buffer.readMatrix(&lm);
     39     return SkLocalMatrixImageFilter::Make(lm, common.getInput(0));
     40 }
     41 
     42 void SkLocalMatrixImageFilter::flatten(SkWriteBuffer& buffer) const {
     43     this->INHERITED::flatten(buffer);
     44     buffer.writeMatrix(fLocalM);
     45 }
     46 
     47 sk_sp<SkSpecialImage> SkLocalMatrixImageFilter::onFilterImage(SkSpecialImage* source,
     48                                                               const Context& ctx,
     49                                                               SkIPoint* offset) const {
     50     Context localCtx(SkMatrix::Concat(ctx.ctm(), fLocalM), ctx.clipBounds(), ctx.cache(),
     51                      ctx.outputProperties());
     52     return this->filterInput(0, source, localCtx, offset);
     53 }
     54 
     55 SkIRect SkLocalMatrixImageFilter::onFilterBounds(const SkIRect& src, const SkMatrix& matrix,
     56                                                  MapDirection direction) const {
     57     return this->getInput(0)->filterBounds(src, SkMatrix::Concat(matrix, fLocalM), direction);
     58 }
     59 
     60 sk_sp<SkImageFilter> SkLocalMatrixImageFilter::onMakeColorSpace(SkColorSpaceXformer* xformer)
     61 const {
     62     SkASSERT(1 == this->countInputs() && this->getInput(0));
     63 
     64     auto input = xformer->apply(this->getInput(0));
     65     if (input.get() != this->getInput(0)) {
     66         return SkLocalMatrixImageFilter::Make(fLocalM, std::move(input));
     67     }
     68     return this->refMe();
     69 }
     70 
     71 #ifndef SK_IGNORE_TO_STRING
     72 void SkLocalMatrixImageFilter::toString(SkString* str) const {
     73     str->append("SkLocalMatrixImageFilter: (");
     74     str->append(")");
     75 }
     76 #endif
     77