|
1 /* |
|
2 * Copyright (c) 2002 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 "CMIDBuffer.h" |
|
19 #include "CMIDToolkit.h" |
|
20 #ifdef RD_JAVA_NGA_ENABLED |
|
21 #include "monitor.h" |
|
22 #endif |
|
23 #include <jdebug.h> |
|
24 |
|
25 #if defined(_DEBUG) && defined(_TRACE) |
|
26 #define TRACE(x) RDebug::Print(_L(x)) |
|
27 #else |
|
28 #define TRACE(x) |
|
29 #endif |
|
30 |
|
31 enum TBufferPriority |
|
32 { |
|
33 EBufferPriorityNormal=0, |
|
34 EBufferPriorityHigh=20, |
|
35 EBufferPriorityRedraw=50, // == EActivePriorityRedrawEvents |
|
36 EBufferPriorityInput=100 // == EActivePriorityWsEvents -> could starve key events. |
|
37 }; |
|
38 |
|
39 |
|
40 // ----------------------------------------------------------------------------- |
|
41 // |
|
42 // ----------------------------------------------------------------------------- |
|
43 TInt CMIDBuffer::New(JNIEnv& aJni, jobject aPeer, TJavaEventServer aServer) |
|
44 { |
|
45 TInt handle; |
|
46 TRAP(handle, |
|
47 TConstructor self(aJni); |
|
48 self->ConstructJniL(aJni,aPeer,aServer); |
|
49 handle = self.GetHandle(); |
|
50 ); |
|
51 return handle; |
|
52 } |
|
53 |
|
54 // ----------------------------------------------------------------------------- |
|
55 // |
|
56 // ----------------------------------------------------------------------------- |
|
57 CMIDBuffer::CMIDBuffer() |
|
58 : iState(EClosed) |
|
59 { |
|
60 iBufferEnd = &iBuffer[ KBufferSize ]; |
|
61 iRead = iBuffer; |
|
62 iReadEnd = iBuffer; |
|
63 iProcEnd = iBuffer; |
|
64 iState = EComplete; |
|
65 } |
|
66 |
|
67 // ----------------------------------------------------------------------------- |
|
68 // |
|
69 // ----------------------------------------------------------------------------- |
|
70 CMIDBuffer::~CMIDBuffer() |
|
71 { |
|
72 #ifdef RD_JAVA_NGA_ENABLED |
|
73 delete iMonitor; |
|
74 #endif |
|
75 } |
|
76 |
|
77 // ----------------------------------------------------------------------------- |
|
78 // |
|
79 // ----------------------------------------------------------------------------- |
|
80 void CMIDBuffer::RequestProcess(JNIEnv& aEnv, jintArray aBuffer, TInt aCount) |
|
81 { |
|
82 ASSERT(aCount <= Size()); |
|
83 |
|
84 aEnv.GetIntArrayRegion(aBuffer, 0, aCount, reinterpret_cast<jint*>(iBuffer)); |
|
85 iRead = &iBuffer[0]; |
|
86 iReadEnd = iRead + aCount; |
|
87 iProcEnd = iRead; |
|
88 iState = ENewRequest; |
|
89 #ifdef RD_JAVA_NGA_ENABLED |
|
90 TBool async = Execute(CMIDBuffer::DoProcess, this); |
|
91 if (async) |
|
92 { |
|
93 iMonitor->wait(); |
|
94 } |
|
95 #else |
|
96 ExecuteTrap(CMIDBuffer::DoProcessL, this); |
|
97 #endif |
|
98 } |
|
99 |
|
100 // ----------------------------------------------------------------------------- |
|
101 // |
|
102 // ----------------------------------------------------------------------------- |
|
103 #ifdef RD_JAVA_NGA_ENABLED |
|
104 TBool CMIDBuffer::DoProcess(CMIDBuffer* aBuffer) |
|
105 { |
|
106 TBool ret = EFalse; |
|
107 TRAPD(err, ret = aBuffer->ProcessL()); |
|
108 if (err != KErrNone) |
|
109 { |
|
110 ret = EFalse; |
|
111 } |
|
112 return ret; |
|
113 } |
|
114 #else |
|
115 void CMIDBuffer::DoProcessL(CMIDBuffer* aBuffer) |
|
116 { |
|
117 aBuffer->ProcessL(); |
|
118 } |
|
119 #endif |
|
120 |
|
121 // ----------------------------------------------------------------------------- |
|
122 // |
|
123 // ----------------------------------------------------------------------------- |
|
124 void CMIDBuffer::ConstructJniL(JNIEnv& aJni, jobject aPeer, TJavaEventServer aServer) |
|
125 { |
|
126 CJavaEventSourceBase::ConstructL(aJni, aPeer, aServer); |
|
127 #ifdef RD_JAVA_NGA_ENABLED |
|
128 try |
|
129 { |
|
130 iMonitor = java::util::Monitor::createMonitor(); |
|
131 } |
|
132 catch (std::exception& e) |
|
133 { |
|
134 User::Leave(KErrNoMemory); |
|
135 } |
|
136 #endif |
|
137 } |
|
138 |
|
139 // ----------------------------------------------------------------------------- |
|
140 // |
|
141 // ----------------------------------------------------------------------------- |
|
142 void CMIDBuffer::FinalizeJni(JNIEnv& /*aEnv*/) |
|
143 { |
|
144 ASSERT(iState == EComplete); |
|
145 iState = EClosed; |
|
146 } |
|
147 |
|
148 // ----------------------------------------------------------------------------- |
|
149 // |
|
150 // ----------------------------------------------------------------------------- |
|
151 #ifdef RD_JAVA_NGA_ENABLED |
|
152 TBool CMIDBuffer::ProcessL() |
|
153 #else |
|
154 void CMIDBuffer::ProcessL() |
|
155 #endif |
|
156 { |
|
157 iCycles = KTotalCycles; |
|
158 |
|
159 #ifdef RD_JAVA_NGA_ENABLED |
|
160 TBool ret = EFalse; |
|
161 #endif |
|
162 |
|
163 if (!iRead->IsActivate()) |
|
164 { |
|
165 ELOG1(EJavaUI, "Buffer didn't start with header! %X", iRead->Header()); |
|
166 User::Leave(KErrCorrupt); |
|
167 } |
|
168 while (iRead < iReadEnd) |
|
169 { |
|
170 const TMIDBufferOp* blockEnd = NULL; |
|
171 |
|
172 // Either setup a new buffer processor, or continue the current |
|
173 // processor after a defer, or a leave. |
|
174 if (iRead->IsActivate()) |
|
175 { |
|
176 MMIDComponent* component = MIDUnhand<MMIDComponent>(iRead->Header()&0x7fffffff); |
|
177 iProcessor = component->Processor(); |
|
178 |
|
179 // Skip activation header. |
|
180 ++iRead; |
|
181 |
|
182 const TInt words = *reinterpret_cast<const TInt*>(iRead); |
|
183 |
|
184 // Skip activation length |
|
185 ++iRead; |
|
186 |
|
187 blockEnd = iRead + words; |
|
188 |
|
189 iProcEnd = blockEnd; |
|
190 |
|
191 ASSERT(iProcEnd <= iReadEnd); |
|
192 } |
|
193 |
|
194 if (iRead < iReadEnd) |
|
195 { |
|
196 #ifdef RD_JAVA_NGA_ENABLED |
|
197 // SYNC or SYNC_RECT op codes are sent to only one object at a time, |
|
198 // so it is safe to have only one boolean indicating async operation. |
|
199 // Currently only Canvas does asyncronous flush. |
|
200 TBool async = iProcessor->ProcessL(iRead, iProcEnd, iCycles, iMonitor); |
|
201 ret |= async; |
|
202 #else |
|
203 iProcessor->ProcessL(iRead, iProcEnd, iCycles); |
|
204 #endif |
|
205 } |
|
206 |
|
207 // Must have reached end of processor range. |
|
208 ASSERT(iRead == iProcEnd); |
|
209 |
|
210 // Next command must be an activate or we must have reached |
|
211 // the end of the buffer. |
|
212 ASSERT((iRead == iReadEnd) || iRead->IsActivate()); |
|
213 } |
|
214 |
|
215 Complete(); |
|
216 |
|
217 #ifdef RD_JAVA_NGA_ENABLED |
|
218 return ret; |
|
219 #endif |
|
220 } |
|
221 |
|
222 // ----------------------------------------------------------------------------- |
|
223 // |
|
224 // ----------------------------------------------------------------------------- |
|
225 void CMIDBuffer::Cancel() |
|
226 { |
|
227 // |
|
228 // Cancel any async operation a processor may be doing. |
|
229 // |
|
230 if (iState == EAsyncPending) |
|
231 { |
|
232 if (iProcessor) |
|
233 { |
|
234 iProcessor->AbortAsync(); |
|
235 } |
|
236 } |
|
237 iState = ECancelled; |
|
238 } |
|
239 |
|
240 |
|
241 // ----------------------------------------------------------------------------- |
|
242 // |
|
243 // ----------------------------------------------------------------------------- |
|
244 void CMIDBuffer::Complete() |
|
245 { |
|
246 ASSERT(iRead == iReadEnd); |
|
247 iRead = iBuffer; |
|
248 iReadEnd = iBuffer; |
|
249 iProcEnd = iBuffer; |
|
250 iState = EComplete; |
|
251 iError = KErrNone; |
|
252 } |
|
253 |
|
254 // ----------------------------------------------------------------------------- |
|
255 // |
|
256 // ----------------------------------------------------------------------------- |
|
257 inline TInt CMIDBuffer::Size() const |
|
258 { |
|
259 return iBufferEnd - iBuffer; |
|
260 } |