|
1 // Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // Graphics Resource - kernel extension implementation |
|
15 // |
|
16 |
|
17 #include <kernel/kern_priv.h> |
|
18 #include "sgextensionimpl.h" |
|
19 |
|
20 DSgExtensionImpl* TheSgExtensionImpl = NULL; |
|
21 |
|
22 DECLARE_STANDARD_EXTENSION() |
|
23 { |
|
24 __KTRACE_OPT(KBOOT, Kern::Printf("Starting Graphics Extension")); |
|
25 TInt err; |
|
26 TheSgExtensionImpl = new DSgExtensionImpl; |
|
27 if (TheSgExtensionImpl) |
|
28 { |
|
29 err = TheSgExtensionImpl->Construct(); |
|
30 if (err != KErrNone) |
|
31 { |
|
32 delete TheSgExtensionImpl; |
|
33 TheSgExtensionImpl = NULL; |
|
34 } |
|
35 } |
|
36 else |
|
37 { |
|
38 err = KErrNoMemory; |
|
39 } |
|
40 if (err != KErrNone) |
|
41 { |
|
42 __KTRACE_OPT(KBOOT, Kern::Printf("Error: Graphics Extension failed to start")); |
|
43 } |
|
44 return err; |
|
45 } |
|
46 |
|
47 EXPORT_C TVersion SgExtension::Version() |
|
48 { |
|
49 return TVersion(1, 1, 1); |
|
50 } |
|
51 |
|
52 EXPORT_C TInt SgExtension::CreateResource(TUint32 aAttribs, const TDesC8& aMetaData, TInt aDataSize, DSgResource*& aResource) |
|
53 { |
|
54 if (!TheSgExtensionImpl) |
|
55 { |
|
56 __KTRACE_OPT(KPANIC, Kern::Printf("Error: Graphics Extension not ready")); |
|
57 return KErrNotReady; |
|
58 } |
|
59 return TheSgExtensionImpl->CreateResource(aAttribs, aMetaData, aDataSize, aResource); |
|
60 } |
|
61 |
|
62 EXPORT_C TInt SgExtension::FindAndOpenResource(TUint64 aId, DSgResource*& aResource) |
|
63 { |
|
64 if (!TheSgExtensionImpl) |
|
65 { |
|
66 __KTRACE_OPT(KPANIC, Kern::Printf("Error: Graphics Extension not ready")); |
|
67 return KErrNotReady; |
|
68 } |
|
69 return TheSgExtensionImpl->FindAndOpenResource(aId, aResource); |
|
70 } |
|
71 |
|
72 EXPORT_C TInt SgExtension::GlobalResourceCount() |
|
73 { |
|
74 if (!TheSgExtensionImpl) |
|
75 { |
|
76 __KTRACE_OPT(KPANIC, Kern::Printf("Error: Graphics Extension not ready")); |
|
77 return KErrNotReady; |
|
78 } |
|
79 return TheSgExtensionImpl->GlobalResourceCount(); |
|
80 } |
|
81 |
|
82 EXPORT_C TInt SgExtension::GlobalGraphicsMemoryUsed() |
|
83 { |
|
84 if (!TheSgExtensionImpl) |
|
85 { |
|
86 __KTRACE_OPT(KPANIC, Kern::Printf("Error: Graphics Extension not ready")); |
|
87 return KErrNotReady; |
|
88 } |
|
89 return TheSgExtensionImpl->GlobalGraphicsMemoryUsed(); |
|
90 } |
|
91 |
|
92 TInt DSgExtensionImpl::Construct() |
|
93 { |
|
94 return Kern::MutexCreate(iMutex, KNullDesC8, KMutexOrdGeneral0); |
|
95 } |
|
96 |
|
97 TInt DSgExtensionImpl::CreateResource(TUint32 aAttribs, const TDesC8& aMetaData, TInt aDataSize, DSgResource*& aResource) |
|
98 { |
|
99 if (aDataSize <= 0) |
|
100 { |
|
101 return KErrArgument; |
|
102 } |
|
103 DSgResourceImpl* resource = static_cast<DSgResourceImpl*>(Kern::Alloc(sizeof(DSgResourceImpl) + aMetaData.Size())); |
|
104 if (!resource) |
|
105 { |
|
106 return KErrNoMemory; |
|
107 } |
|
108 TChunkCreateInfo info; |
|
109 info.iType = TChunkCreateInfo::ESharedKernelMultiple; |
|
110 info.iMaxSize = aDataSize; |
|
111 info.iOwnsMemory = ETrue; |
|
112 #ifdef __WINS__ |
|
113 info.iMapAttr = aAttribs, 0; // suppress compiler warning: cache attribute does not apply to WINS |
|
114 #else |
|
115 info.iMapAttr = aAttribs & ESgResourceAttrCpuCached ? EMapAttrCachedMax : EMapAttrL1Uncached; |
|
116 #endif |
|
117 DChunk* chunk; |
|
118 TLinAddr kernAddr; |
|
119 TUint32 mapAttr; |
|
120 TInt err = Kern::ChunkCreate(info, chunk, kernAddr, mapAttr); |
|
121 if (err != KErrNone) |
|
122 { |
|
123 Kern::Free(resource); |
|
124 return err; |
|
125 } |
|
126 err = Kern::ChunkCommit(chunk, 0, aDataSize); |
|
127 if (err != KErrNone) |
|
128 { |
|
129 Kern::ChunkClose(chunk); |
|
130 Kern::Free(resource); |
|
131 return err; |
|
132 } |
|
133 Kern::MutexWait(*iMutex); |
|
134 new(resource) DSgResourceImpl(*this, ++iLastResourceId, aAttribs, aMetaData, chunk, aDataSize); |
|
135 err = iResources.InsertInOrder(resource, DSgResource::Compare); |
|
136 Kern::MutexSignal(*iMutex); |
|
137 if (err != KErrNone) |
|
138 { |
|
139 resource->Close(); |
|
140 } |
|
141 aResource = (err == KErrNone ? resource : NULL); |
|
142 return err; |
|
143 } |
|
144 |
|
145 TInt DSgExtensionImpl::FindAndOpenResource(TUint64 aId, DSgResource*& aResource) |
|
146 { |
|
147 if (aId == 0) |
|
148 { |
|
149 return KErrArgument; |
|
150 } |
|
151 Kern::MutexWait(*iMutex); |
|
152 TInt i = iResources.FindInOrder(aId, DSgResource::Compare); |
|
153 if (i < 0) |
|
154 { |
|
155 Kern::MutexSignal(*iMutex); |
|
156 return KErrNotFound; |
|
157 } |
|
158 DSgResource* resource = iResources[i]; |
|
159 TInt err = resource->Open(); |
|
160 Kern::MutexSignal(*iMutex); |
|
161 aResource = (err == KErrNone ? resource : NULL); |
|
162 return err; |
|
163 } |
|
164 |
|
165 void DSgExtensionImpl::DeleteResource(DSgResource* aResource) |
|
166 { |
|
167 Kern::MutexWait(*iMutex); |
|
168 TInt i = iResources.FindInOrder(aResource, DSgResource::Compare); |
|
169 if (i >= 0) |
|
170 { |
|
171 iResources.Remove(i); |
|
172 } |
|
173 else |
|
174 { |
|
175 __KTRACE_OPT(KPANIC, Kern::Printf("Error: internal state of Graphics Extension is inconsistent")); |
|
176 } |
|
177 Kern::MutexSignal(*iMutex); |
|
178 delete aResource; |
|
179 } |
|
180 |
|
181 TInt DSgExtensionImpl::GlobalResourceCount() const |
|
182 { |
|
183 return iResources.Count(); |
|
184 } |
|
185 |
|
186 TInt DSgExtensionImpl::GlobalGraphicsMemoryUsed() const |
|
187 { |
|
188 TInt ret = 0; |
|
189 Kern::MutexWait(*iMutex); |
|
190 TInt n = iResources.Count(); |
|
191 for (TInt i = 0; i < n; ++i) |
|
192 { |
|
193 ret += iResources[i]->DataChunk()->Size(); |
|
194 } |
|
195 Kern::MutexSignal(*iMutex); |
|
196 return ret; |
|
197 } |