summaryrefslogtreecommitdiffstats
path: root/media/libstagefright/filters/IntrinsicBlurFilter.cpp
blob: cbcf6992bd257bbdb05c0200e34f3cd79623697c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//#define LOG_NDEBUG 0
#define LOG_TAG "IntrinsicBlurFilter"

#include <utils/Log.h>

#include <media/stagefright/foundation/ABuffer.h>
#include <media/stagefright/foundation/ADebug.h>
#include <media/stagefright/foundation/AMessage.h>

#include "IntrinsicBlurFilter.h"

namespace android {

status_t IntrinsicBlurFilter::configure(const sp<AMessage> &msg) {
    status_t err = SimpleFilter::configure(msg);
    if (err != OK) {
        return err;
    }

    if (!msg->findString("cacheDir", &mCacheDir)) {
        ALOGE("Failed to find cache directory in config message.");
        return NAME_NOT_FOUND;
    }

    return OK;
}

status_t IntrinsicBlurFilter::start() {
    // TODO: use a single RS context object for entire application
    mRS = new RSC::RS();

    if (!mRS->init(mCacheDir.c_str())) {
        ALOGE("Failed to initialize RenderScript context.");
        return NO_INIT;
    }

    // 32-bit elements for ARGB8888
    RSC::sp<const RSC::Element> e = RSC::Element::U8_4(mRS);

    RSC::Type::Builder tb(mRS, e);
    tb.setX(mWidth);
    tb.setY(mHeight);
    RSC::sp<const RSC::Type> t = tb.create();

    mAllocIn = RSC::Allocation::createTyped(mRS, t);
    mAllocOut = RSC::Allocation::createTyped(mRS, t);

    mBlur = RSC::ScriptIntrinsicBlur::create(mRS, e);
    mBlur->setRadius(mBlurRadius);
    mBlur->setInput(mAllocIn);

    return OK;
}

void IntrinsicBlurFilter::reset() {
    mBlur.clear();
    mAllocOut.clear();
    mAllocIn.clear();
    mRS.clear();
}

status_t IntrinsicBlurFilter::setParameters(const sp<AMessage> &msg) {
    sp<AMessage> params;
    CHECK(msg->findMessage("params", &params));

    float blurRadius;
    if (params->findFloat("blur-radius", &blurRadius)) {
        mBlurRadius = blurRadius;
    }

    return OK;
}

status_t IntrinsicBlurFilter::processBuffers(
        const sp<ABuffer> &srcBuffer, const sp<ABuffer> &outBuffer) {
    mAllocIn->copy1DRangeFrom(0, mWidth * mHeight, srcBuffer->data());
    mBlur->forEach(mAllocOut);
    mAllocOut->copy1DRangeTo(0, mWidth * mHeight, outBuffer->data());

    return OK;
}

}   // namespace android