AndroidImageProxy.java revision 6e981cafa9cc57ebc7100cb84f68fb74dbd163da
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
17package com.android.camera.one.v2.camera2proxy;
18
19import android.graphics.Rect;
20import android.media.Image;
21
22import java.nio.ByteBuffer;
23import java.util.ArrayList;
24import java.util.List;
25
26/**
27 * An {@link ImageProxy} backed by an {@link android.media.Image}.
28 */
29public class AndroidImageProxy implements ImageProxy {
30
31    /**
32     * An {@link ImageProxy.Plane} backed by an
33     * {@link android.media.Image.Plane}.
34     */
35    public class Plane implements ImageProxy.Plane {
36        private final Image.Plane mPlane;
37
38        public Plane(Image.Plane imagePlane) {
39            mPlane = imagePlane;
40        }
41
42        /**
43         * @see {@link android.media.Image.Plane#getRowStride}
44         */
45        @Override
46        public int getRowStride() {
47            return mPlane.getRowStride();
48        }
49
50        /**
51         * @see {@link android.media.Image.Plane#getPixelStride}
52         */
53        @Override
54        public int getPixelStride() {
55            return mPlane.getPixelStride();
56        }
57
58        /**
59         * @see {@link android.media.Image.Plane#getBuffer}
60         */
61        @Override
62        public ByteBuffer getBuffer() {
63            return mPlane.getBuffer();
64        }
65
66    }
67
68    private final android.media.Image mImage;
69
70    public AndroidImageProxy(android.media.Image image) {
71        mImage = image;
72    }
73
74    /**
75     * @see {@link android.media.Image#getCropRect}
76     */
77    @Override
78    public Rect getCropRect() {
79        return mImage.getCropRect();
80    }
81
82    /**
83     * @see {@link android.media.Image#setCropRect}
84     */
85    @Override
86    public void setCropRect(Rect cropRect) {
87        mImage.setCropRect(cropRect);
88    }
89
90    /**
91     * @see {@link android.media.Image#getFormat}
92     */
93    @Override
94    public int getFormat() {
95        return mImage.getFormat();
96    }
97
98    /**
99     * @see {@link android.media.Image#getHeight}
100     */
101    @Override
102    public int getHeight() {
103        return mImage.getHeight();
104    }
105
106    /**
107     * <p>
108     * NOTE:This wrapper is functionally correct, but has some performance
109     * implications: it dynamically allocates a small array (usually 1-3
110     * elements) and iteratively constructs each element of this array every
111     * time it is called. This function definitely should <b>NOT</b> be called
112     * within an tight inner loop, as it may litter the GC with lots of little
113     * allocations. However, a proper caching of this object needs to be tied to
114     * the Android Image updates, which would be a little more complex than this
115     * object needs to be. So, just consider the performance when using this
116     * function wrapper.
117     * </p>
118     * @see {@link android.media.Image#getPlanes}
119     */
120    @Override
121    public List<ImageProxy.Plane> getPlanes() {
122        Image.Plane[] planes = mImage.getPlanes();
123
124        List<ImageProxy.Plane> wrappedPlanes = new ArrayList<>(planes.length);
125
126        for (int i = 0; i < planes.length; i++) {
127            wrappedPlanes.add(new Plane(planes[i]));
128        }
129        return wrappedPlanes;
130    }
131
132    /**
133     * @see {@link android.media.Image#getTimestamp}
134     */
135    @Override
136    public long getTimestamp() {
137        return mImage.getTimestamp();
138    }
139
140    /**
141     * @see {@link android.media.Image#getWidth}
142     */
143    @Override
144    public int getWidth() {
145        return mImage.getWidth();
146    }
147
148    /**
149     * @see {@link android.media.Image#close}
150     */
151    @Override
152    public void close() {
153        mImage.close();
154    }
155}
156