1/*
2 * Copyright (C) 2011 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
17package androidx.media.filterfw;
18
19import android.annotation.TargetApi;
20import android.renderscript.Allocation;
21
22import java.nio.ByteBuffer;
23import java.nio.ByteOrder;
24
25public class FrameBuffer1D extends Frame {
26
27    private int mLength = 0;
28
29    /**
30     * Access frame's data using a {@link ByteBuffer}.
31     * This is a convenience method and is equivalent to calling {@code lockData} with an
32     * {@code accessFormat} of {@code ACCESS_BYTES}.
33     * When writing to the {@link ByteBuffer}, the byte order should be always set to
34     * {@link ByteOrder#nativeOrder()}.
35     *
36     * @return The byte buffer instance holding the Frame's data.
37     */
38    public ByteBuffer lockBytes(int mode) {
39        assertAccessible(mode);
40        return (ByteBuffer)mBackingStore.lockData(mode, BackingStore.ACCESS_BYTES);
41    }
42
43    /**
44     * Access frame's data using a RenderScript {@link Allocation}.
45     * This is a convenience method and is equivalent to calling {@code lockData} with an
46     * {@code accessFormat} of {@code ACCESS_ALLOCATION}.
47     *
48     * @return The Allocation instance holding the Frame's data.
49     */
50    @TargetApi(11)
51    public Allocation lockAllocation(int mode) {
52        assertAccessible(mode);
53        return (Allocation) mBackingStore.lockData(mode, BackingStore.ACCESS_ALLOCATION);
54    }
55
56    public int getLength() {
57        return mLength;
58    }
59
60    @Override
61    public int[] getDimensions() {
62        return super.getDimensions();
63    }
64
65    /**
66     * TODO: Documentation. Note that frame contents are invalidated.
67     */
68    @Override
69    public void resize(int[] newDimensions) {
70        super.resize(newDimensions);
71    }
72
73    static FrameBuffer1D create(BackingStore backingStore) {
74        assertCanCreate(backingStore);
75        return new FrameBuffer1D(backingStore);
76    }
77
78    FrameBuffer1D(BackingStore backingStore) {
79        super(backingStore);
80        updateLength(backingStore.getDimensions());
81    }
82
83    static void assertCanCreate(BackingStore backingStore) {
84        FrameType type = backingStore.getFrameType();
85        if (type.getElementSize() == 0) {
86            throw new RuntimeException("Cannot access Frame of type " + type + " as a FrameBuffer "
87                + "instance!");
88        }
89        int[] dims = backingStore.getDimensions();
90        if (dims == null || dims.length == 0) {
91            throw new RuntimeException("Cannot access Frame with no dimensions as a FrameBuffer "
92                + "instance!");
93        }
94    }
95
96    void updateLength(int[] dimensions) {
97        mLength = 1;
98        for (int dim : dimensions) {
99            mLength *= dim;
100        }
101    }
102}
103
104