blob: af2f2d7bbe5d85640cc0362a8eb183e9f03b6b02 [file] [log] [blame]
James Dong79f407c2011-05-05 12:50:04 -07001/*
2 * Copyright 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
17#ifndef _ANDROID_MEDIA_UTILS_H_
18#define _ANDROID_MEDIA_UTILS_H_
19
Jaesung Chung8409c062016-01-19 10:48:30 +090020#include "src/piex_types.h"
21#include "src/piex.h"
James Dong79f407c2011-05-05 12:50:04 -070022
Jaesung Chung8409c062016-01-19 10:48:30 +090023#include <android_runtime/AndroidRuntime.h>
Jaesung Chung6e08d2b2016-02-25 15:04:56 +090024#include <gui/CpuConsumer.h>
Jaesung Chung8409c062016-01-19 10:48:30 +090025#include <jni.h>
26#include <JNIHelp.h>
James Dong79f407c2011-05-05 12:50:04 -070027#include <utils/KeyedVector.h>
28#include <utils/String8.h>
Jaesung Chung6e08d2b2016-02-25 15:04:56 +090029#include <SkStream.h>
James Dong79f407c2011-05-05 12:50:04 -070030
31namespace android {
32
Jaesung Chung6e08d2b2016-02-25 15:04:56 +090033class AssetStream : public piex::StreamInterface {
34private:
35 SkStream *mStream;
36 size_t mPosition;
37
38public:
Chih-Hung Hsiehbd423492016-08-29 14:49:57 -070039 explicit AssetStream(SkStream* stream);
Jaesung Chung6e08d2b2016-02-25 15:04:56 +090040 ~AssetStream();
41
42 // Reads 'length' amount of bytes from 'offset' to 'data'. The 'data' buffer
43 // provided by the caller, guaranteed to be at least "length" bytes long.
44 // On 'kOk' the 'data' pointer contains 'length' valid bytes beginning at
45 // 'offset' bytes from the start of the stream.
46 // Returns 'kFail' if 'offset' + 'length' exceeds the stream and does not
47 // change the contents of 'data'.
48 piex::Error GetData(
49 const size_t offset, const size_t length, std::uint8_t* data) override;
50};
51
52class BufferedStream : public piex::StreamInterface {
53private:
54 SkStream *mStream;
55 // Growable memory stream
56 SkDynamicMemoryWStream mStreamBuffer;
57
58 // Minimum size to read on filling the buffer.
59 const size_t kMinSizeToRead = 8192;
60
61public:
Chih-Hung Hsiehbd423492016-08-29 14:49:57 -070062 explicit BufferedStream(SkStream* stream);
Jaesung Chung6e08d2b2016-02-25 15:04:56 +090063 ~BufferedStream();
64
65 // Reads 'length' amount of bytes from 'offset' to 'data'. The 'data' buffer
66 // provided by the caller, guaranteed to be at least "length" bytes long.
67 // On 'kOk' the 'data' pointer contains 'length' valid bytes beginning at
68 // 'offset' bytes from the start of the stream.
69 // Returns 'kFail' if 'offset' + 'length' exceeds the stream and does not
70 // change the contents of 'data'.
71 piex::Error GetData(
72 const size_t offset, const size_t length, std::uint8_t* data) override;
73};
74
Jaesung Chung8409c062016-01-19 10:48:30 +090075class FileStream : public piex::StreamInterface {
76private:
77 FILE *mFile;
78 size_t mPosition;
Jaesung Chung8409c062016-01-19 10:48:30 +090079
80public:
Chih-Hung Hsiehbd423492016-08-29 14:49:57 -070081 explicit FileStream(const int fd);
82 explicit FileStream(const String8 filename);
Jaesung Chung8409c062016-01-19 10:48:30 +090083 ~FileStream();
84
85 // Reads 'length' amount of bytes from 'offset' to 'data'. The 'data' buffer
86 // provided by the caller, guaranteed to be at least "length" bytes long.
87 // On 'kOk' the 'data' pointer contains 'length' valid bytes beginning at
88 // 'offset' bytes from the start of the stream.
89 // Returns 'kFail' if 'offset' + 'length' exceeds the stream and does not
90 // change the contents of 'data'.
91 piex::Error GetData(
92 const size_t offset, const size_t length, std::uint8_t* data) override;
93 bool exists() const;
Jaesung Chung8409c062016-01-19 10:48:30 +090094};
95
96// Reads EXIF metadata from a given raw image via piex.
97// And returns true if the operation is successful; otherwise, false.
98bool GetExifFromRawImage(
Jaesung Chung6e08d2b2016-02-25 15:04:56 +090099 piex::StreamInterface* stream, const String8& filename, piex::PreviewImageData& image_data);
Jaesung Chung8409c062016-01-19 10:48:30 +0900100
101// Returns true if the conversion is successful; otherwise, false.
James Dong79f407c2011-05-05 12:50:04 -0700102bool ConvertKeyValueArraysToKeyedVector(
Jaesung Chung8409c062016-01-19 10:48:30 +0900103 JNIEnv *env, jobjectArray keys, jobjectArray values,
104 KeyedVector<String8, String8>* vector);
James Dong79f407c2011-05-05 12:50:04 -0700105
Andreas Huber88572f72012-02-21 11:47:18 -0800106struct AMessage;
107status_t ConvertMessageToMap(
108 JNIEnv *env, const sp<AMessage> &msg, jobject *map);
109
110status_t ConvertKeyValueArraysToMessage(
111 JNIEnv *env, jobjectArray keys, jobjectArray values,
112 sp<AMessage> *msg);
113
Zhijun He0ab41622016-02-25 16:00:38 -0800114// -----------Utility functions used by ImageReader/Writer JNI-----------------
115
116typedef CpuConsumer::LockedBuffer LockedImage;
117
118bool usingRGBAToJpegOverride(int32_t imageFormat, int32_t containerFormat);
119
120int32_t applyFormatOverrides(int32_t imageFormat, int32_t containerFormat);
121
122uint32_t Image_getJpegSize(LockedImage* buffer, bool usingRGBAOverride);
123
124bool isFormatOpaque(int format);
125
126bool isPossiblyYUV(PixelFormat format);
127
128status_t getLockedImageInfo(LockedImage* buffer, int idx, int32_t containerFormat,
129 uint8_t **base, uint32_t *size, int *pixelStride, int *rowStride);
130
131status_t lockImageFromBuffer(sp<GraphicBuffer> buffer, uint32_t inUsage,
132 const Rect& rect, int fenceFd, LockedImage* outputImage);
133
134status_t lockImageFromBuffer(BufferItem* bufferItem, uint32_t inUsage,
135 int fenceFd, LockedImage* outputImage);
136
137int getBufferWidth(BufferItem *buffer);
138
139int getBufferHeight(BufferItem *buffer);
140
James Dong79f407c2011-05-05 12:50:04 -0700141}; // namespace android
142
143#endif // _ANDROID_MEDIA_UTILS_H_