IntrinsicBlurFilter.cpp revision 6dad9ad21ff547fab7f192956e4a3732fd78488b
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "IntrinsicBlurFilter"
19
20#include <utils/Log.h>
21
22#include <media/stagefright/foundation/ABuffer.h>
23#include <media/stagefright/foundation/ADebug.h>
24#include <media/stagefright/foundation/AMessage.h>
25
26#include "IntrinsicBlurFilter.h"
27
28namespace android {
29
30status_t IntrinsicBlurFilter::configure(const sp<AMessage> &msg) {
31    status_t err = SimpleFilter::configure(msg);
32    if (err != OK) {
33        return err;
34    }
35
36    if (!msg->findString("cacheDir", &mCacheDir)) {
37        ALOGE("Failed to find cache directory in config message.");
38        return NAME_NOT_FOUND;
39    }
40
41    return OK;
42}
43
44status_t IntrinsicBlurFilter::start() {
45    // TODO: use a single RS context object for entire application
46    mRS = new RSC::RS();
47
48    if (!mRS->init(mCacheDir.c_str())) {
49        ALOGE("Failed to initialize RenderScript context.");
50        return NO_INIT;
51    }
52
53    // 32-bit elements for ARGB8888
54    RSC::sp<const RSC::Element> e = RSC::Element::U8_4(mRS);
55
56    RSC::Type::Builder tb(mRS, e);
57    tb.setX(mWidth);
58    tb.setY(mHeight);
59    RSC::sp<const RSC::Type> t = tb.create();
60
61    mAllocIn = RSC::Allocation::createTyped(mRS, t);
62    mAllocOut = RSC::Allocation::createTyped(mRS, t);
63
64    mBlur = RSC::ScriptIntrinsicBlur::create(mRS, e);
65    mBlur->setRadius(mBlurRadius);
66    mBlur->setInput(mAllocIn);
67
68    return OK;
69}
70
71void IntrinsicBlurFilter::reset() {
72    mBlur.clear();
73    mAllocOut.clear();
74    mAllocIn.clear();
75    mRS.clear();
76}
77
78status_t IntrinsicBlurFilter::setParameters(const sp<AMessage> &msg) {
79    sp<AMessage> params;
80    CHECK(msg->findMessage("params", &params));
81
82    float blurRadius;
83    if (params->findFloat("blur-radius", &blurRadius)) {
84        mBlurRadius = blurRadius;
85    }
86
87    return OK;
88}
89
90status_t IntrinsicBlurFilter::processBuffers(
91        const sp<ABuffer> &srcBuffer, const sp<ABuffer> &outBuffer) {
92    mAllocIn->copy1DRangeFrom(0, mWidth * mHeight, srcBuffer->data());
93    mBlur->forEach(mAllocOut);
94    mAllocOut->copy1DRangeTo(0, mWidth * mHeight, outBuffer->data());
95
96    return OK;
97}
98
99}   // namespace android
100