IntrinsicBlurFilter.cpp revision 744f5739019d1fd917f981e740b353c3d73fd1a8
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::start() {
31    // TODO: use a single RS context object for entire application
32    mRS = new RSC::RS();
33
34    // only legitimate because this is a standalone executable
35    // TODO: do we need to dynamically determine the cache directory?
36    if (!mRS->init("/system/bin")) {
37        ALOGE("Failed to initialize RenderScript context.");
38        return NO_INIT;
39    }
40
41    // 32-bit elements for ARGB8888
42    RSC::sp<const RSC::Element> e = RSC::Element::U8_4(mRS);
43
44    RSC::Type::Builder tb(mRS, e);
45    tb.setX(mWidth);
46    tb.setY(mHeight);
47    RSC::sp<const RSC::Type> t = tb.create();
48
49    mAllocIn = RSC::Allocation::createTyped(mRS, t);
50    mAllocOut = RSC::Allocation::createTyped(mRS, t);
51
52    mBlur = RSC::ScriptIntrinsicBlur::create(mRS, e);
53    mBlur->setRadius(mBlurRadius);
54    mBlur->setInput(mAllocIn);
55
56    return OK;
57}
58
59void IntrinsicBlurFilter::reset() {
60    mBlur.clear();
61    mAllocOut.clear();
62    mAllocIn.clear();
63    mRS.clear();
64}
65
66status_t IntrinsicBlurFilter::setParameters(const sp<AMessage> &msg) {
67    sp<AMessage> params;
68    CHECK(msg->findMessage("params", &params));
69
70    float blurRadius;
71    if (params->findFloat("blur-radius", &blurRadius)) {
72        mBlurRadius = blurRadius;
73    }
74
75    return OK;
76}
77
78status_t IntrinsicBlurFilter::processBuffers(
79        const sp<ABuffer> &srcBuffer, const sp<ABuffer> &outBuffer) {
80    mAllocIn->copy1DRangeFrom(0, mWidth * mHeight, srcBuffer->data());
81    mBlur->forEach(mAllocOut);
82    mAllocOut->copy1DRangeTo(0, mWidth * mHeight, outBuffer->data());
83
84    return OK;
85}
86
87}   // namespace android
88