|
1 /* |
|
2 * Copyright (c) 2007 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: Graphics context |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 #include <alf/alfgc.h> |
|
21 #include <alf/alfenv.h> |
|
22 #include <alf/alftexture.h> |
|
23 #include <alf/alftimedvalue.h> |
|
24 #include <alf/alftextstyle.h> |
|
25 #include <alf/alfrealline.h> |
|
26 #include "alflogger.h" |
|
27 |
|
28 //#include <uiacceltk/huicanvasconstants.h> |
|
29 |
|
30 |
|
31 enum TAlfCanvasCommands |
|
32 { |
|
33 EAlfCanvasDrawImage, |
|
34 EAlfCanvasDrawText, |
|
35 EAlfCanvasDrawLines, |
|
36 EAlfCanvasDrawEllipse, |
|
37 EAlfCanvasDrawPoints, |
|
38 EAlfCanvasDrawPolygon, |
|
39 EAlfCanvasDrawRects, |
|
40 |
|
41 // Setters |
|
42 EAlfCanvasSetTextStyle, |
|
43 EAlfCanvasSetPenColor, |
|
44 EAlfCanvasSetPenWidth, |
|
45 EAlfCanvasSetOpacity, |
|
46 EAlfCanvasSetPolygonDrawMode, |
|
47 EAlfCanvasSetTextAlign, |
|
48 |
|
49 // Transformations |
|
50 EAlfCanvasLoadIdentity, |
|
51 EAlfCanvasTranslate, |
|
52 EAlfCanvasScale, |
|
53 EAlfCanvasRotate, |
|
54 |
|
55 EAlfCanvasPrepareDrawText |
|
56 }; |
|
57 |
|
58 NONSHARABLE_STRUCT( TAlfCanvasEndDrawParams ) |
|
59 { |
|
60 TInt iDummy1; |
|
61 TInt iDummy2; |
|
62 TInt iDummy3; |
|
63 TInt iDummy4; |
|
64 }; |
|
65 |
|
66 NONSHARABLE_STRUCT( TAlfCanvasDrawImageParams ) |
|
67 { |
|
68 TInt iTexture; |
|
69 TReal32 iDestinationRectTopLeftX; |
|
70 TReal32 iDestinationRectTopLeftY; |
|
71 TReal32 iDestinationRectBottomRightX; |
|
72 TReal32 iDestinationRectBottomRightY; |
|
73 }; |
|
74 |
|
75 NONSHARABLE_STRUCT( TAlfCanvasDrawTextParams ) |
|
76 { |
|
77 TReal32 iDestinationRectTopLeftX; |
|
78 TReal32 iDestinationRectTopLeftY; |
|
79 TReal32 iDestinationRectBottomRightX; |
|
80 TReal32 iDestinationRectBottomRightY; |
|
81 }; |
|
82 |
|
83 NONSHARABLE_STRUCT( TAlfCanvasDrawLinesParams ) |
|
84 { |
|
85 TInt iLineCount; |
|
86 RArray<TReal32> iPointCords; |
|
87 }; |
|
88 |
|
89 NONSHARABLE_STRUCT( TAlfCanvasDrawEllipseParams ) |
|
90 { |
|
91 TReal32 iDestinationRectTopLeftX; |
|
92 TReal32 iDestinationRectTopLeftY; |
|
93 TReal32 iDestinationRectBottomRightX; |
|
94 TReal32 iDestinationRectBottomRightY; |
|
95 }; |
|
96 |
|
97 |
|
98 // Setters |
|
99 NONSHARABLE_STRUCT( TAlfCanvasSetTextStyleParams ) |
|
100 { |
|
101 TInt iTextstyleId; |
|
102 }; |
|
103 |
|
104 NONSHARABLE_STRUCT( TAlfCanvasSetPenColorParams ) |
|
105 { |
|
106 TRgb iPenColor; |
|
107 }; |
|
108 NONSHARABLE_STRUCT( TAlfCanvasSetPenWidthParams ) |
|
109 { |
|
110 TReal32 iPenWidth; |
|
111 }; |
|
112 NONSHARABLE_STRUCT( TAlfCanvasSetOpacityParams ) |
|
113 { |
|
114 TReal32 iOpacity; |
|
115 }; |
|
116 NONSHARABLE_STRUCT( TAlfCanvasSetPolygonDrawModeParams ) |
|
117 { |
|
118 TInt iPolygonDrawMode; |
|
119 }; |
|
120 NONSHARABLE_STRUCT( TAlfCanvasSetTextAlignParams ) |
|
121 { |
|
122 TInt iTextAlignHorizontal; |
|
123 TInt iTextAlignVertical; |
|
124 }; |
|
125 |
|
126 |
|
127 // Transformations |
|
128 NONSHARABLE_STRUCT( TAlfCanvasLoadIdentityParams ) |
|
129 { |
|
130 TInt iDummy; |
|
131 }; |
|
132 |
|
133 NONSHARABLE_STRUCT( TAlfCanvasTranslateParams ) |
|
134 { |
|
135 TReal32 iX; |
|
136 TReal32 iY; |
|
137 TReal32 iZ; |
|
138 }; |
|
139 |
|
140 NONSHARABLE_STRUCT( TAlfCanvasScaleParams ) |
|
141 { |
|
142 TReal32 iX; |
|
143 TReal32 iY; |
|
144 TReal32 iZ; |
|
145 }; |
|
146 |
|
147 NONSHARABLE_STRUCT( TAlfCanvasRotateParams ) |
|
148 { |
|
149 TReal32 iAngle; |
|
150 TReal32 iX; |
|
151 TReal32 iY; |
|
152 TReal32 iZ; |
|
153 }; |
|
154 |
|
155 |
|
156 template <class T> |
|
157 HBufC8* AlfGcExternalizeL( const RArray<T>& aArray ) |
|
158 { |
|
159 const TInt itemCount = aArray.Count(); |
|
160 if ( !itemCount) |
|
161 { |
|
162 return HBufC8::NewL(0); |
|
163 } |
|
164 |
|
165 const T* firstItem = &aArray[0]; |
|
166 TPtrC8 arrayPtr( (TUint8*)firstItem, itemCount*sizeof(T) ); |
|
167 HBufC8* buffer = HBufC8::NewL( sizeof(TInt) + arrayPtr.Length() ); |
|
168 TPtr8 ptr = buffer->Des(); |
|
169 |
|
170 ptr.Append( (const TUint8*)&itemCount, sizeof(TInt) ); |
|
171 ptr.Append( arrayPtr ); |
|
172 return buffer; |
|
173 } |
|
174 |
|
175 |
|
176 const TInt KAlfGcBufferGranularity = 1024; |
|
177 |
|
178 struct CAlfGc::TAlfGcData |
|
179 { |
|
180 TAlfGcData() : iBuffer(NULL) {} |
|
181 HBufC8* iBuffer; |
|
182 }; |
|
183 |
|
184 // ======== MEMBER FUNCTIONS ======== |
|
185 |
|
186 // --------------------------------------------------------------------------- |
|
187 // |
|
188 // --------------------------------------------------------------------------- |
|
189 // |
|
190 CAlfGc* CAlfGc::NewL() |
|
191 { |
|
192 CAlfGc* self = new (ELeave) CAlfGc; |
|
193 CleanupStack::PushL( self ); |
|
194 self->ConstructL(); |
|
195 CleanupStack::Pop( self ); |
|
196 return self; |
|
197 } |
|
198 |
|
199 CAlfGc::CAlfGc() |
|
200 { |
|
201 } |
|
202 |
|
203 void CAlfGc::ConstructL() |
|
204 { |
|
205 iData = new (ELeave) TAlfGcData; |
|
206 } |
|
207 |
|
208 CAlfGc::~CAlfGc() |
|
209 { |
|
210 if ( iData ) |
|
211 { |
|
212 delete iData->iBuffer; |
|
213 iData->iBuffer = NULL; |
|
214 } |
|
215 delete iData; |
|
216 iData = NULL; |
|
217 } |
|
218 |
|
219 |
|
220 void CAlfGc::ActivateL() |
|
221 { |
|
222 Deactivate(); // Just in case... |
|
223 iData->iBuffer = HBufC8::NewL( KAlfGcBufferGranularity ); |
|
224 } |
|
225 |
|
226 HBufC8* CAlfGc::CommandBuffer() |
|
227 { |
|
228 return iData->iBuffer; |
|
229 } |
|
230 |
|
231 void CAlfGc::Deactivate() |
|
232 { |
|
233 delete iData->iBuffer; |
|
234 iData->iBuffer = NULL; |
|
235 } |
|
236 |
|
237 void CAlfGc::AppendCommand( TInt aCommand, const TDesC8& aParams ) |
|
238 { |
|
239 TRAP_IGNORE(AppendCommandL(aCommand,aParams)); |
|
240 } |
|
241 |
|
242 void CAlfGc::AppendCommandL( TInt aCommand, const TDesC8& aParams ) |
|
243 { |
|
244 if (!iData->iBuffer) |
|
245 { |
|
246 return; |
|
247 } |
|
248 |
|
249 __ALFLOGSTRING1( "CAlfGc::AppendCommand - start, buffer length: %d", iData->iBuffer->Length() ) |
|
250 |
|
251 // create a bigger buffer if needed |
|
252 const TInt newSpaceNeeded = 4 + 4 + aParams.Length() + 4; |
|
253 __ALFLOGSTRING1( "CAlfGc::AppendCommand - new space needed: %d", newSpaceNeeded) |
|
254 if ( iData->iBuffer->Des().MaxLength() - iData->iBuffer->Length() < newSpaceNeeded ) |
|
255 { |
|
256 __ALFLOGSTRING1( "CAlfGc::AppendCommand - allocating new buffer length: %d", iData->iBuffer->Length() + newSpaceNeeded * 2 ) |
|
257 iData->iBuffer = iData->iBuffer->ReAllocL( iData->iBuffer->Length() + newSpaceNeeded * 2 ); |
|
258 } |
|
259 |
|
260 TPtr8 ptr = iData->iBuffer->Des(); |
|
261 TInt length = aParams.Length(); |
|
262 TInt command = aCommand; |
|
263 |
|
264 ptr.Append( (const TUint8*)&command, sizeof(TInt) ); |
|
265 ptr.Append( (const TUint8*)&length, sizeof(TInt) ); |
|
266 ptr.Append( aParams ); |
|
267 |
|
268 TInt padding = 0; |
|
269 if (length % 4) |
|
270 { |
|
271 padding = 4 - length % 4; |
|
272 } |
|
273 |
|
274 for ( TInt i = 0 ; i < padding ; i++ ) |
|
275 { |
|
276 ptr.Append( 0xFF ); |
|
277 } |
|
278 |
|
279 for ( TInt i = 0 ; i < aParams.Length() ; i++ ) |
|
280 { |
|
281 __ALFLOGSTRING2( "[%d]: %d", i, aParams[i] ) |
|
282 } |
|
283 |
|
284 __ALFLOGSTRING1( "CAlfGc::AppendCommand - end, buffer length: %d", iData->iBuffer->Length() ) |
|
285 } |
|
286 |
|
287 // --------------------------------------------------------------------------- |
|
288 // |
|
289 // --------------------------------------------------------------------------- |
|
290 // |
|
291 void CAlfGc::DrawImage(const CAlfTexture& aTexture, const TAlfRealRect& aDestinationRect) |
|
292 { |
|
293 TInt serversideTexture = aTexture.ServerHandle(); |
|
294 |
|
295 const TAlfCanvasDrawImageParams params = {serversideTexture, |
|
296 aDestinationRect.iTl.iX, |
|
297 aDestinationRect.iTl.iY, |
|
298 aDestinationRect.iBr.iX, |
|
299 aDestinationRect.iBr.iY |
|
300 }; |
|
301 TPckgC<TAlfCanvasDrawImageParams> paramsPckg( params ); |
|
302 AppendCommand( EAlfCanvasDrawImage, paramsPckg ); |
|
303 } |
|
304 |
|
305 // --------------------------------------------------------------------------- |
|
306 // |
|
307 // --------------------------------------------------------------------------- |
|
308 // |
|
309 void CAlfGc::DrawText(const TDesC& aText, const TAlfRealRect& aDestinationRect) |
|
310 { |
|
311 TPtrC8 ptr(reinterpret_cast<const TUint8 *>(aText.Ptr()), aText.Length()*2); |
|
312 AppendCommand( EAlfCanvasPrepareDrawText, ptr ); |
|
313 |
|
314 const TAlfCanvasDrawTextParams params = { |
|
315 aDestinationRect.iTl.iX, |
|
316 aDestinationRect.iTl.iY, |
|
317 aDestinationRect.iBr.iX, |
|
318 aDestinationRect.iBr.iY |
|
319 }; |
|
320 TPckgC<TAlfCanvasDrawTextParams> paramsPckg( params ); |
|
321 AppendCommand( EAlfCanvasDrawText, paramsPckg ); |
|
322 } |
|
323 |
|
324 void CAlfGc::DrawLines(const RArray<TReal32> & aLinesPointCords) |
|
325 { |
|
326 HBufC8* buffer = NULL; |
|
327 TRAP_IGNORE(buffer = AlfGcExternalizeL(aLinesPointCords)) |
|
328 if (buffer) |
|
329 { |
|
330 AppendCommand( EAlfCanvasDrawLines, *buffer ); |
|
331 delete buffer; |
|
332 buffer = NULL; |
|
333 } |
|
334 } |
|
335 |
|
336 |
|
337 void CAlfGc::DrawEllipse(const TAlfRealRect& aDestinationRect) |
|
338 { |
|
339 const TAlfCanvasDrawEllipseParams params = { |
|
340 aDestinationRect.iTl.iX, |
|
341 aDestinationRect.iTl.iY, |
|
342 aDestinationRect.iBr.iX, |
|
343 aDestinationRect.iBr.iY |
|
344 }; |
|
345 TPckgC<TAlfCanvasDrawEllipseParams> paramsPckg( params ); |
|
346 AppendCommand( EAlfCanvasDrawEllipse, paramsPckg ); |
|
347 } |
|
348 |
|
349 void CAlfGc::DrawPoints(const RArray<TReal32> & aPointsCords) |
|
350 { |
|
351 HBufC8* buffer = NULL; |
|
352 TRAP_IGNORE(buffer = AlfGcExternalizeL(aPointsCords)) |
|
353 if (buffer) |
|
354 { |
|
355 AppendCommand( EAlfCanvasDrawPoints, *buffer ); |
|
356 delete buffer; |
|
357 buffer = NULL; |
|
358 } |
|
359 } |
|
360 |
|
361 void CAlfGc::DrawPolygon(const RArray<TReal32> & aPolygonPointCords) |
|
362 { |
|
363 HBufC8* buffer = NULL; |
|
364 TRAP_IGNORE(buffer = AlfGcExternalizeL(aPolygonPointCords)) |
|
365 if (buffer) |
|
366 { |
|
367 AppendCommand( EAlfCanvasDrawPolygon, *buffer ); |
|
368 delete buffer; |
|
369 buffer = NULL; |
|
370 } |
|
371 } |
|
372 |
|
373 void CAlfGc::DrawRects(const RArray<TReal32> & aRectPointCords) |
|
374 { |
|
375 HBufC8* buffer = NULL; |
|
376 TRAP_IGNORE(buffer = AlfGcExternalizeL(aRectPointCords)) |
|
377 if (buffer) |
|
378 { |
|
379 AppendCommand( EAlfCanvasDrawRects, *buffer ); |
|
380 delete buffer; |
|
381 buffer = NULL; |
|
382 } |
|
383 } |
|
384 |
|
385 // --------------------------------------------------------------------------- |
|
386 // |
|
387 // --------------------------------------------------------------------------- |
|
388 // |
|
389 void CAlfGc::SetTextStyle(const CAlfTextStyle& aTextStyle) |
|
390 { |
|
391 TInt serverSideTextStyleId = aTextStyle.ServerHandle(); |
|
392 TAlfCanvasSetTextStyleParams params = {serverSideTextStyleId}; |
|
393 TPckgC<TAlfCanvasSetTextStyleParams> paramsPckg( params ); |
|
394 AppendCommand( EAlfCanvasSetTextStyle, paramsPckg ); |
|
395 } |
|
396 |
|
397 |
|
398 void CAlfGc::SetPenColor(const TRgb& aColor) |
|
399 { |
|
400 TAlfCanvasSetPenColorParams params = {aColor}; |
|
401 TPckgC<TAlfCanvasSetPenColorParams> paramsPckg( params ); |
|
402 AppendCommand( EAlfCanvasSetPenColor, paramsPckg ); |
|
403 } |
|
404 |
|
405 void CAlfGc::SetPenWidth(const TReal32& aWidth) |
|
406 { |
|
407 TAlfCanvasSetPenWidthParams params = {aWidth}; |
|
408 TPckgC<TAlfCanvasSetPenWidthParams> paramsPckg( params ); |
|
409 AppendCommand( EAlfCanvasSetPenWidth, paramsPckg ); |
|
410 } |
|
411 |
|
412 void CAlfGc::SetOpacity(const TReal32& aOpacity) |
|
413 { |
|
414 TAlfCanvasSetOpacityParams params = {aOpacity}; |
|
415 TPckgC<TAlfCanvasSetOpacityParams> paramsPckg( params ); |
|
416 AppendCommand( EAlfCanvasSetOpacity, paramsPckg ); |
|
417 } |
|
418 |
|
419 |
|
420 void CAlfGc::SetPolygonDrawMode(TAlfPolygonDrawMode aPolygonDrawMode) |
|
421 { |
|
422 TAlfCanvasSetPolygonDrawModeParams params = {aPolygonDrawMode}; |
|
423 TPckgC<TAlfCanvasSetPolygonDrawModeParams> paramsPckg( params ); |
|
424 AppendCommand( EAlfCanvasSetPolygonDrawMode, paramsPckg ); |
|
425 } |
|
426 void CAlfGc::SetTextAlign(TAlfAlignHorizontal aAlignHorizontal, TAlfAlignVertical aAlignVertical) |
|
427 { |
|
428 TAlfCanvasSetTextAlignParams params = {aAlignHorizontal, aAlignVertical}; |
|
429 TPckgC<TAlfCanvasSetTextAlignParams> paramsPckg( params ); |
|
430 AppendCommand( EAlfCanvasSetTextAlign, paramsPckg ); |
|
431 } |
|
432 |
|
433 // Transformations |
|
434 void CAlfGc::LoadIdentity() |
|
435 { |
|
436 TAlfCanvasLoadIdentityParams params = {0}; |
|
437 TPckgC<TAlfCanvasLoadIdentityParams> paramsPckg( params ); |
|
438 AppendCommand( EAlfCanvasLoadIdentity, paramsPckg ); |
|
439 } |
|
440 |
|
441 void CAlfGc::Translate(const TReal32& aX, const TReal32& aY, const TReal32& aZ) |
|
442 { |
|
443 TAlfCanvasTranslateParams params = {aX, aY, aZ}; |
|
444 TPckgC<TAlfCanvasTranslateParams> paramsPckg( params ); |
|
445 AppendCommand( EAlfCanvasTranslate, paramsPckg ); |
|
446 } |
|
447 |
|
448 void CAlfGc::Scale(const TReal32& aX, const TReal32& aY, const TReal32& aZ) |
|
449 { |
|
450 TAlfCanvasScaleParams params = {aX, aY, aZ}; |
|
451 TPckgC<TAlfCanvasScaleParams> paramsPckg( params ); |
|
452 AppendCommand( EAlfCanvasScale, paramsPckg ); |
|
453 } |
|
454 |
|
455 void CAlfGc::Rotate(const TReal32& aAngle, const TReal32& aX, const TReal32& aY, const TReal32& aZ) |
|
456 { |
|
457 TAlfCanvasRotateParams params = {aAngle, aX, aY, aZ}; |
|
458 TPckgC<TAlfCanvasRotateParams> paramsPckg( params ); |
|
459 AppendCommand( EAlfCanvasRotate, paramsPckg ); |
|
460 } |