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 "SaturationFilter"
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 "SaturationFilter.h"
27
28namespace android {
29
30status_t SaturationFilter::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 SaturationFilter::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    mScript = new ScriptC_saturationARGB(mRS);
65
66    mScript->set_gSaturation(mSaturation);
67
68    return OK;
69}
70
71void SaturationFilter::reset() {
72    mScript.clear();
73    mAllocOut.clear();
74    mAllocIn.clear();
75    mRS.clear();
76}
77
78status_t SaturationFilter::setParameters(const sp<AMessage> &msg) {
79    sp<AMessage> params;
80    CHECK(msg->findMessage("params", &params));
81
82    float saturation;
83    if (params->findFloat("saturation", &saturation)) {
84        mSaturation = saturation;
85    }
86
87    return OK;
88}
89
90status_t SaturationFilter::processBuffers(
91        const sp<ABuffer> &srcBuffer, const sp<ABuffer> &outBuffer) {
92    mAllocIn->copy1DRangeFrom(0, mWidth * mHeight, srcBuffer->data());
93    mScript->forEach_root(mAllocIn, mAllocOut);
94    mAllocOut->copy1DRangeTo(0, mWidth * mHeight, outBuffer->data());
95
96    return OK;
97}
98
99}   // namespace android
100