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.filterpacks.transform;
18
19import androidx.media.filterfw.Frame;
20import androidx.media.filterfw.FrameImage2D;
21import androidx.media.filterfw.FrameType;
22import androidx.media.filterfw.TextureSource;
23
24import java.util.Arrays;
25
26/** Internal class that contains utility functions used by the transform filters. **/
27class TransformUtils {
28
29    public static int powOf2(int x) {
30        --x;
31        // Fill with 1s
32        x |= x >> 1;
33        x |= x >> 2;
34        x |= x >> 4;
35        x |= x >> 8;
36        x |= x >> 16;
37        // Next int is now pow-of-2
38        return x + 1;
39    }
40
41    public static FrameImage2D makeMipMappedFrame(FrameImage2D current, int[] dimensions) {
42        // Note: Future versions of GLES will support NPOT mipmapping. When these become more
43        // widely used, we can add a check here to disable frame expansion on such devices.
44        int[] pow2Dims = new int[] { powOf2(dimensions[0]), powOf2(dimensions[1]) };
45        if (current == null) {
46            FrameType imageType = FrameType.image2D(FrameType.ELEMENT_RGBA8888,
47                                                    FrameType.READ_GPU | FrameType.WRITE_GPU);
48            current = Frame.create(imageType, pow2Dims).asFrameImage2D();
49        } else if (!Arrays.equals(dimensions, current.getDimensions())) {
50            current.resize(pow2Dims);
51        }
52        return current;
53    }
54
55    public static FrameImage2D makeTempFrame(FrameImage2D current, int[] dimensions) {
56        if (current == null) {
57            FrameType imageType = FrameType.image2D(FrameType.ELEMENT_RGBA8888,
58                                                    FrameType.READ_GPU | FrameType.WRITE_GPU);
59            current = Frame.create(imageType, dimensions).asFrameImage2D();
60        } else if (!Arrays.equals(dimensions, current.getDimensions())) {
61            current.resize(dimensions);
62        }
63        return current;
64    }
65
66    public static void generateMipMaps(FrameImage2D frame) {
67        TextureSource texture = frame.lockTextureSource();
68        texture.generateMipmaps();
69        frame.unlock();
70    }
71
72    public static void setTextureParameter(FrameImage2D frame, int param, int value) {
73        TextureSource texture = frame.lockTextureSource();
74        texture.setParameter(param, value);
75        frame.unlock();
76    }
77
78}
79