SkImageFilter.cpp revision c2e8cef4792b478547973d312b26fff4aab7c729
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
14SK_DEFINE_INST_COUNT(SkImageFilter)
15
16SkImageFilter::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
24SkImageFilter::SkImageFilter(SkImageFilter* input)
25  : fInputCount(1), fInputs(new SkImageFilter*[1]) {
26    fInputs[0] = input;
27    SkSafeRef(fInputs[0]);
28}
29
30SkImageFilter::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
38SkImageFilter::~SkImageFilter() {
39    for (int i = 0; i < fInputCount; i++) {
40        SkSafeUnref(fInputs[i]);
41    }
42    delete[] fInputs;
43}
44
45SkImageFilter::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
56void 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
67SkBitmap 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
81bool 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
94bool 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
101bool SkImageFilter::onFilterImage(Proxy*, const SkBitmap&, const SkMatrix&,
102                                  SkBitmap*, SkIPoint*) {
103    return false;
104}
105
106bool SkImageFilter::canFilterImageGPU() const {
107    return false;
108}
109
110GrTexture* SkImageFilter::onFilterImageGPU(Proxy* proxy, GrTexture* texture, const SkRect& rect) {
111    return NULL;
112}
113
114bool SkImageFilter::onFilterBounds(const SkIRect& src, const SkMatrix& ctm,
115                                   SkIRect* dst) {
116    *dst = src;
117    return true;
118}
119
120bool SkImageFilter::asNewCustomStage(GrCustomStage**, GrTexture*) const {
121    return false;
122}
123
124SkColorFilter* SkImageFilter::asColorFilter() const {
125    return NULL;
126}
127