blob: 8184f943ce23a0153b0c8e68b72b50bc0026d575 [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 <camera3.h>
25#include <gui/CpuConsumer.h>
Jaesung Chung8409c062016-01-19 10:48:30 +090026#include <jni.h>
27#include <JNIHelp.h>
James Dong79f407c2011-05-05 12:50:04 -070028#include <utils/KeyedVector.h>
29#include <utils/String8.h>
Jaesung Chung6e08d2b2016-02-25 15:04:56 +090030#include <SkStream.h>
James Dong79f407c2011-05-05 12:50:04 -070031
32namespace android {
33
Jaesung Chung6e08d2b2016-02-25 15:04:56 +090034class AssetStream : public piex::StreamInterface {
35private:
36 SkStream *mStream;
37 size_t mPosition;
38
39public:
40 AssetStream(SkStream* stream);
41 ~AssetStream();
42
43 // Reads 'length' amount of bytes from 'offset' to 'data'. The 'data' buffer
44 // provided by the caller, guaranteed to be at least "length" bytes long.
45 // On 'kOk' the 'data' pointer contains 'length' valid bytes beginning at
46 // 'offset' bytes from the start of the stream.
47 // Returns 'kFail' if 'offset' + 'length' exceeds the stream and does not
48 // change the contents of 'data'.
49 piex::Error GetData(
50 const size_t offset, const size_t length, std::uint8_t* data) override;
51};
52
53class BufferedStream : public piex::StreamInterface {
54private:
55 SkStream *mStream;
56 // Growable memory stream
57 SkDynamicMemoryWStream mStreamBuffer;
58
59 // Minimum size to read on filling the buffer.
60 const size_t kMinSizeToRead = 8192;
61
62public:
63 BufferedStream(SkStream* stream);
64 ~BufferedStream();
65
66 // Reads 'length' amount of bytes from 'offset' to 'data'. The 'data' buffer
67 // provided by the caller, guaranteed to be at least "length" bytes long.
68 // On 'kOk' the 'data' pointer contains 'length' valid bytes beginning at
69 // 'offset' bytes from the start of the stream.
70 // Returns 'kFail' if 'offset' + 'length' exceeds the stream and does not
71 // change the contents of 'data'.
72 piex::Error GetData(
73 const size_t offset, const size_t length, std::uint8_t* data) override;
74};
75
Jaesung Chung8409c062016-01-19 10:48:30 +090076class FileStream : public piex::StreamInterface {
77private:
78 FILE *mFile;
79 size_t mPosition;
Jaesung Chung8409c062016-01-19 10:48:30 +090080
81public:
Jaesung Chung15ef59e2016-02-25 14:12:32 +000082 FileStream(const int fd);
Jaesung Chung8409c062016-01-19 10:48:30 +090083 FileStream(const String8 filename);
84 ~FileStream();
85
86 // Reads 'length' amount of bytes from 'offset' to 'data'. The 'data' buffer
87 // provided by the caller, guaranteed to be at least "length" bytes long.
88 // On 'kOk' the 'data' pointer contains 'length' valid bytes beginning at
89 // 'offset' bytes from the start of the stream.
90 // Returns 'kFail' if 'offset' + 'length' exceeds the stream and does not
91 // change the contents of 'data'.
92 piex::Error GetData(
93 const size_t offset, const size_t length, std::uint8_t* data) override;
94 bool exists() const;
Jaesung Chung8409c062016-01-19 10:48:30 +090095};
96
97// Reads EXIF metadata from a given raw image via piex.
98// And returns true if the operation is successful; otherwise, false.
99bool GetExifFromRawImage(
Jaesung Chung6e08d2b2016-02-25 15:04:56 +0900100 piex::StreamInterface* stream, const String8& filename, piex::PreviewImageData& image_data);
Jaesung Chung8409c062016-01-19 10:48:30 +0900101
102// Returns true if the conversion is successful; otherwise, false.
James Dong79f407c2011-05-05 12:50:04 -0700103bool ConvertKeyValueArraysToKeyedVector(
Jaesung Chung8409c062016-01-19 10:48:30 +0900104 JNIEnv *env, jobjectArray keys, jobjectArray values,
105 KeyedVector<String8, String8>* vector);
James Dong79f407c2011-05-05 12:50:04 -0700106
Andreas Huber88572f72012-02-21 11:47:18 -0800107struct AMessage;
108status_t ConvertMessageToMap(
109 JNIEnv *env, const sp<AMessage> &msg, jobject *map);
110
111status_t ConvertKeyValueArraysToMessage(
112 JNIEnv *env, jobjectArray keys, jobjectArray values,
113 sp<AMessage> *msg);
114
Zhijun He0ab41622016-02-25 16:00:38 -0800115// -----------Utility functions used by ImageReader/Writer JNI-----------------
116
117typedef CpuConsumer::LockedBuffer LockedImage;
118
119bool usingRGBAToJpegOverride(int32_t imageFormat, int32_t containerFormat);
120
121int32_t applyFormatOverrides(int32_t imageFormat, int32_t containerFormat);
122
123uint32_t Image_getJpegSize(LockedImage* buffer, bool usingRGBAOverride);
124
125bool isFormatOpaque(int format);
126
127bool isPossiblyYUV(PixelFormat format);
128
129status_t getLockedImageInfo(LockedImage* buffer, int idx, int32_t containerFormat,
130 uint8_t **base, uint32_t *size, int *pixelStride, int *rowStride);
131
132status_t lockImageFromBuffer(sp<GraphicBuffer> buffer, uint32_t inUsage,
133 const Rect& rect, int fenceFd, LockedImage* outputImage);
134
135status_t lockImageFromBuffer(BufferItem* bufferItem, uint32_t inUsage,
136 int fenceFd, LockedImage* outputImage);
137
138int getBufferWidth(BufferItem *buffer);
139
140int getBufferHeight(BufferItem *buffer);
141
James Dong79f407c2011-05-05 12:50:04 -0700142}; // namespace android
143
144#endif // _ANDROID_MEDIA_UTILS_H_