Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright 2012 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 "SkImageFilter.h"
      9 
     10 #include "SkBitmap.h"
     11 #include "SkFlattenableBuffers.h"
     12 #include "SkRect.h"
     13 
     14 SK_DEFINE_INST_COUNT(SkImageFilter)
     15 
     16 SkImageFilter::SkImageFilter(int inputCount, SkImageFilter** inputs)
     17   : fInputCount(inputCount), fInputs(new SkImageFilter*[inputCount]) {
     18     for (int i = 0; i < inputCount; ++i) {
     19         fInputs[i] = inputs[i];
     20         SkSafeRef(fInputs[i]);
     21     }
     22 }
     23 
     24 SkImageFilter::SkImageFilter(SkImageFilter* input)
     25   : fInputCount(1), fInputs(new SkImageFilter*[1]) {
     26     fInputs[0] = input;
     27     SkSafeRef(fInputs[0]);
     28 }
     29 
     30 SkImageFilter::SkImageFilter(SkImageFilter* input1, SkImageFilter* input2)
     31   : fInputCount(2), fInputs(new SkImageFilter*[2]) {
     32     fInputs[0] = input1;
     33     fInputs[1] = input2;
     34     SkSafeRef(fInputs[0]);
     35     SkSafeRef(fInputs[1]);
     36 }
     37 
     38 SkImageFilter::~SkImageFilter() {
     39     for (int i = 0; i < fInputCount; i++) {
     40         SkSafeUnref(fInputs[i]);
     41     }
     42     delete[] fInputs;
     43 }
     44 
     45 SkImageFilter::SkImageFilter(SkFlattenableReadBuffer& buffer)
     46     : fInputCount(buffer.readInt()), fInputs(new SkImageFilter*[fInputCount]) {
     47     for (int i = 0; i < fInputCount; i++) {
     48         if (buffer.readBool()) {
     49             fInputs[i] = static_cast<SkImageFilter*>(buffer.readFlattenable());
     50         } else {
     51             fInputs[i] = NULL;
     52         }
     53     }
     54 }
     55 
     56 void SkImageFilter::flatten(SkFlattenableWriteBuffer& buffer) const {
     57     buffer.writeInt(fInputCount);
     58     for (int i = 0; i < fInputCount; i++) {
     59         SkImageFilter* input = getInput(i);
     60         buffer.writeBool(input != NULL);
     61         if (input != NULL) {
     62             buffer.writeFlattenable(input);
     63         }
     64     }
     65 }
     66 
     67 SkBitmap SkImageFilter::getInputResult(int index, Proxy* proxy,
     68                                        const SkBitmap& src, const SkMatrix& ctm,
     69                                        SkIPoint* loc) {
     70     SkASSERT(index < fInputCount);
     71     SkImageFilter* input = getInput(index);
     72     SkBitmap result;
     73     if (input && input->filterImage(proxy, src, ctm, &result, loc)) {
     74         return result;
     75     } else {
     76         return src;
     77     }
     78 }
     79 
     80 
     81 bool SkImageFilter::filterImage(Proxy* proxy, const SkBitmap& src,
     82                                 const SkMatrix& ctm,
     83                                 SkBitmap* result, SkIPoint* loc) {
     84     SkASSERT(result);
     85     SkASSERT(loc);
     86     /*
     87      *  Give the proxy first shot at the filter. If it returns false, ask
     88      *  the filter to do it.
     89      */
     90     return (proxy && proxy->filterImage(this, src, ctm, result, loc)) ||
     91            this->onFilterImage(proxy, src, ctm, result, loc);
     92 }
     93 
     94 bool SkImageFilter::filterBounds(const SkIRect& src, const SkMatrix& ctm,
     95                                  SkIRect* dst) {
     96     SkASSERT(&src);
     97     SkASSERT(dst);
     98     return this->onFilterBounds(src, ctm, dst);
     99 }
    100 
    101 bool SkImageFilter::onFilterImage(Proxy*, const SkBitmap&, const SkMatrix&,
    102                                   SkBitmap*, SkIPoint*) {
    103     return false;
    104 }
    105 
    106 bool SkImageFilter::canFilterImageGPU() const {
    107     return false;
    108 }
    109 
    110 bool SkImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, SkBitmap* result) {
    111     SkASSERT(false);  // Should never be called, since canFilterImageGPU() returned false.
    112     return false;
    113 }
    114 
    115 bool SkImageFilter::onFilterBounds(const SkIRect& src, const SkMatrix& ctm,
    116                                    SkIRect* dst) {
    117     *dst = src;
    118     return true;
    119 }
    120 
    121 bool SkImageFilter::asNewEffect(GrEffectRef**, GrTexture*) const {
    122     return false;
    123 }
    124 
    125 SkColorFilter* SkImageFilter::asColorFilter() const {
    126     return NULL;
    127 }
    128