|
1 /* |
|
2 * Copyright (c) 2004-2005 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 #include "CMIDImageLoader.h" |
|
19 #include <jni.h> |
|
20 |
|
21 const TInt KInitialBufferSize = 1024*32; // 32kb |
|
22 |
|
23 // |
|
24 // |
|
25 // |
|
26 class CAsyncDecodeEvent : public CJavaEvent<CMIDImageLoader> |
|
27 { |
|
28 public: |
|
29 CAsyncDecodeEvent(jmethodID aMethod); |
|
30 |
|
31 void SetResult(jint aResult); |
|
32 void Dispatch(JNIEnv& aJni); |
|
33 |
|
34 private: |
|
35 jmethodID iMethod; |
|
36 jint iResult; |
|
37 }; |
|
38 |
|
39 CAsyncDecodeEvent::CAsyncDecodeEvent(jmethodID aMethod) |
|
40 : iMethod(aMethod) |
|
41 , iResult(KErrGeneral) |
|
42 { |
|
43 } |
|
44 |
|
45 void CAsyncDecodeEvent::SetResult(jint aResult) |
|
46 { |
|
47 iResult=aResult; |
|
48 } |
|
49 |
|
50 void CAsyncDecodeEvent::Dispatch(JNIEnv& aJni) |
|
51 { |
|
52 jobject peerObject = aJni.NewLocalRef(Object().Peer()); |
|
53 if (peerObject) |
|
54 { |
|
55 aJni.CallVoidMethod(peerObject, iMethod, iResult); |
|
56 aJni.DeleteLocalRef(peerObject); |
|
57 } |
|
58 } |
|
59 |
|
60 TInt CMIDImageLoader::New(JNIEnv& aJni, jobject aPeer, TJavaEventServer aServer, jint aToolkitHandle) |
|
61 { |
|
62 TInt handle; |
|
63 TRAP(handle, |
|
64 TConstructor self(aJni); |
|
65 self->ConstructJniL(aJni,aPeer,aServer, JavaUnhand<CMIDToolkit>(aToolkitHandle)); |
|
66 handle = self.GetHandle(); |
|
67 ); |
|
68 return handle; |
|
69 } |
|
70 |
|
71 CMIDImageLoader::CMIDImageLoader() |
|
72 { |
|
73 } |
|
74 |
|
75 CMIDImageLoader::~CMIDImageLoader() |
|
76 { |
|
77 ASSERT(NULL == iNotifyEvent); |
|
78 ASSERT(NULL == iDecoder); |
|
79 ASSERT(NULL == iBufferArray); |
|
80 } |
|
81 |
|
82 void CMIDImageLoader::ConstructJniL(JNIEnv& aJni, jobject aPeer, TJavaEventServer aServer, CMIDToolkit* aToolkit) |
|
83 { |
|
84 CJavaEventSourceBase::ConstructL(aJni, aPeer, aServer); |
|
85 iBufferArray = (TUint8*)User::AllocL(KInitialBufferSize); |
|
86 iBufferLength = KInitialBufferSize; |
|
87 iBufferCount = 0; |
|
88 User::LeaveIfError(ExecuteTrap(InvokeConstructSvrL, this, aToolkit)); |
|
89 } |
|
90 |
|
91 void CMIDImageLoader::InvokeConstructSvrL(CMIDImageLoader* aLoader, CMIDToolkit* aToolkit) |
|
92 { |
|
93 aLoader->ConstructSvrL(*aToolkit); |
|
94 } |
|
95 |
|
96 void CMIDImageLoader::ConstructSvrL(CMIDToolkit& aToolkit) |
|
97 { |
|
98 iDecoder = aToolkit.GraphicsFactory().NewImageDecoderL(); |
|
99 } |
|
100 |
|
101 void CMIDImageLoader::FinalizeSvr() |
|
102 { |
|
103 if (iDecoder) |
|
104 { |
|
105 iDecoder->Dispose(); |
|
106 iDecoder = NULL; |
|
107 } |
|
108 } |
|
109 |
|
110 void CMIDImageLoader::FinalizeJni(JNIEnv& /*aJni*/) |
|
111 { |
|
112 delete iNotifyEvent; |
|
113 iNotifyEvent = NULL; |
|
114 User::Free(iBufferArray); |
|
115 iBufferArray = NULL; |
|
116 } |
|
117 |
|
118 /** |
|
119 * Append data to decoder buffer. Currently we just extract the bytes from the java array |
|
120 * and do an ipc here for each block. We could choose buffer up client side until the buffer |
|
121 * is full and then flush. This would require keeping track of the byte count. |
|
122 */ |
|
123 TInt CMIDImageLoader::AppendJni(JNIEnv& aJni, jbyteArray aByteArray, jint aByteOffset, jint aByteCount) |
|
124 { |
|
125 if (iBufferLength < aByteCount) |
|
126 { |
|
127 TUint8* array = (TUint8*)User::ReAlloc(iBufferArray, aByteCount); |
|
128 if (array) |
|
129 { |
|
130 iBufferArray = array; |
|
131 iBufferLength = aByteCount; |
|
132 } |
|
133 else |
|
134 { |
|
135 return KErrNoMemory; |
|
136 } |
|
137 } |
|
138 |
|
139 ASSERT(iBufferLength >= aByteCount); |
|
140 |
|
141 aJni.GetByteArrayRegion(aByteArray, aByteOffset, aByteCount, (jbyte*)(iBufferArray)); |
|
142 |
|
143 // |
|
144 // No deferred flush yet. |
|
145 // |
|
146 ASSERT(iBufferCount == 0); |
|
147 iBufferCount = aByteCount; |
|
148 TInt err = ExecuteTrap(InvokeAppendSvrL, this); |
|
149 iBufferCount = 0; |
|
150 |
|
151 return err; |
|
152 } |
|
153 |
|
154 void CMIDImageLoader::AppendSvrL() |
|
155 { |
|
156 TPtrC8 bufferPtr(iBufferArray, iBufferCount); |
|
157 iDecoder->AppendL(bufferPtr); |
|
158 } |
|
159 |
|
160 void CMIDImageLoader::InvokeAppendSvrL(CMIDImageLoader* aLoader) |
|
161 { |
|
162 aLoader->AppendSvrL(); |
|
163 } |
|
164 |
|
165 void CMIDImageLoader::InvokeAsyncDecodeSvrL(CMIDImageLoader* aLoader) |
|
166 { |
|
167 aLoader->AsyncDecodeSvrL(); |
|
168 } |
|
169 |
|
170 /** |
|
171 * Initiate asynchronous decode. |
|
172 */ |
|
173 TInt CMIDImageLoader::AsyncDecodeJni(JNIEnv& aJni) |
|
174 { |
|
175 jobject peerObj = aJni.NewLocalRef(Peer()); |
|
176 |
|
177 if (peerObj) |
|
178 { |
|
179 jclass clazz = aJni.GetObjectClass(peerObj); |
|
180 jmethodID method = aJni.GetMethodID(clazz,"signal","(I)V"); |
|
181 aJni.DeleteLocalRef(peerObj); |
|
182 if (!iNotifyEvent) |
|
183 { |
|
184 iNotifyEvent = new CAsyncDecodeEvent(method); |
|
185 } |
|
186 if (!iNotifyEvent) |
|
187 { |
|
188 return KErrNoMemory; |
|
189 } |
|
190 return ExecuteTrap(InvokeAsyncDecodeSvrL, this); |
|
191 } |
|
192 return -1; |
|
193 } |
|
194 |
|
195 void CMIDImageLoader::AsyncDecodeSvrL() |
|
196 { |
|
197 iDecoder->DecodeL(this); |
|
198 } |
|
199 |
|
200 /** |
|
201 * |
|
202 */ |
|
203 MMIDImageDecoder* CMIDImageLoader::Decoder() |
|
204 { |
|
205 return iDecoder; |
|
206 } |
|
207 |
|
208 void CMIDImageLoader::DecodeComplete(TInt aError) |
|
209 { |
|
210 CAsyncDecodeEvent* event = iNotifyEvent; |
|
211 iNotifyEvent = NULL; |
|
212 ASSERT(event); |
|
213 event->SetResult(aError); |
|
214 PostEvent(event,CJavaEventBase::ENotifyPriority); |
|
215 } |