OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #include "GrContext.h" | 10 #include "GrContext.h" |
11 | 11 |
12 #include "effects/GrConfigConversionEffect.h" | 12 #include "effects/GrConfigConversionEffect.h" |
13 #include "effects/GrDashingEffect.h" | 13 #include "effects/GrDashingEffect.h" |
14 #include "effects/GrSingleTextureEffect.h" | 14 #include "effects/GrSingleTextureEffect.h" |
15 | 15 |
16 #include "GrAARectRenderer.h" | 16 #include "GrAARectRenderer.h" |
17 #include "GrBufferAllocPool.h" | 17 #include "GrBufferAllocPool.h" |
18 #include "GrGpu.h" | 18 #include "GrGpu.h" |
19 #include "GrDistanceFieldTextContext.h" | 19 #include "GrDistanceFieldTextContext.h" |
20 #include "GrDrawTargetCaps.h" | 20 #include "GrDrawTargetCaps.h" |
21 #include "GrIndexBuffer.h" | 21 #include "GrIndexBuffer.h" |
22 #include "GrInOrderDrawBuffer.h" | 22 #include "GrInOrderDrawBuffer.h" |
23 #include "GrLayerCache.h" | 23 #include "GrLayerCache.h" |
24 #include "GrOvalRenderer.h" | 24 #include "GrOvalRenderer.h" |
25 #include "GrPathRenderer.h" | 25 #include "GrPathRenderer.h" |
26 #include "GrPathUtils.h" | 26 #include "GrPathUtils.h" |
27 #include "GrResourceCache.h" | 27 #include "GrResourceCache.h" |
28 #include "GrResourceCache2.h" | |
29 #include "GrSoftwarePathRenderer.h" | 28 #include "GrSoftwarePathRenderer.h" |
30 #include "GrStencilBuffer.h" | 29 #include "GrStencilBuffer.h" |
31 #include "GrStencilAndCoverTextContext.h" | 30 #include "GrStencilAndCoverTextContext.h" |
32 #include "GrStrokeInfo.h" | 31 #include "GrStrokeInfo.h" |
33 #include "GrTextStrike.h" | 32 #include "GrTextStrike.h" |
34 #include "GrTraceMarker.h" | 33 #include "GrTraceMarker.h" |
35 #include "GrTracing.h" | 34 #include "GrTracing.h" |
36 #include "SkDashPathPriv.h" | 35 #include "SkDashPathPriv.h" |
37 #include "SkGr.h" | 36 #include "SkGr.h" |
38 #include "SkRTConf.h" | 37 #include "SkRTConf.h" |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 } | 101 } |
103 } | 102 } |
104 | 103 |
105 GrContext::GrContext(const Options& opts) : fOptions(opts) { | 104 GrContext::GrContext(const Options& opts) : fOptions(opts) { |
106 fDrawState = NULL; | 105 fDrawState = NULL; |
107 fGpu = NULL; | 106 fGpu = NULL; |
108 fClip = NULL; | 107 fClip = NULL; |
109 fPathRendererChain = NULL; | 108 fPathRendererChain = NULL; |
110 fSoftwarePathRenderer = NULL; | 109 fSoftwarePathRenderer = NULL; |
111 fResourceCache = NULL; | 110 fResourceCache = NULL; |
112 fResourceCache2 = NULL; | |
113 fFontCache = NULL; | 111 fFontCache = NULL; |
114 fDrawBuffer = NULL; | 112 fDrawBuffer = NULL; |
115 fDrawBufferVBAllocPool = NULL; | 113 fDrawBufferVBAllocPool = NULL; |
116 fDrawBufferIBAllocPool = NULL; | 114 fDrawBufferIBAllocPool = NULL; |
117 fFlushToReduceCacheSize = false; | 115 fFlushToReduceCacheSize = false; |
118 fAARectRenderer = NULL; | 116 fAARectRenderer = NULL; |
119 fOvalRenderer = NULL; | 117 fOvalRenderer = NULL; |
120 fViewMatrix.reset(); | 118 fViewMatrix.reset(); |
121 fMaxTextureSizeOverride = 1 << 20; | 119 fMaxTextureSizeOverride = 1 << 20; |
122 } | 120 } |
123 | 121 |
124 bool GrContext::init(GrBackend backend, GrBackendContext backendContext) { | 122 bool GrContext::init(GrBackend backend, GrBackendContext backendContext) { |
125 SkASSERT(NULL == fGpu); | 123 SkASSERT(NULL == fGpu); |
126 | 124 |
127 fGpu = GrGpu::Create(backend, backendContext, this); | 125 fGpu = GrGpu::Create(backend, backendContext, this); |
128 if (NULL == fGpu) { | 126 if (NULL == fGpu) { |
129 return false; | 127 return false; |
130 } | 128 } |
131 | 129 |
132 fDrawState = SkNEW(GrDrawState); | 130 fDrawState = SkNEW(GrDrawState); |
133 fGpu->setDrawState(fDrawState); | 131 fGpu->setDrawState(fDrawState); |
134 | 132 |
135 fResourceCache = SkNEW_ARGS(GrResourceCache, (MAX_RESOURCE_CACHE_COUNT, | 133 fResourceCache = SkNEW_ARGS(GrResourceCache, (MAX_RESOURCE_CACHE_COUNT, |
136 MAX_RESOURCE_CACHE_BYTES)); | 134 MAX_RESOURCE_CACHE_BYTES)); |
137 fResourceCache->setOverbudgetCallback(OverbudgetCB, this); | 135 fResourceCache->setOverbudgetCallback(OverbudgetCB, this); |
138 fResourceCache2 = SkNEW(GrResourceCache2); | |
139 | 136 |
140 fFontCache = SkNEW_ARGS(GrFontCache, (fGpu)); | 137 fFontCache = SkNEW_ARGS(GrFontCache, (fGpu)); |
141 | 138 |
142 fLayerCache.reset(SkNEW_ARGS(GrLayerCache, (this))); | 139 fLayerCache.reset(SkNEW_ARGS(GrLayerCache, (this))); |
143 | 140 |
144 fLastDrawWasBuffered = kNo_BufferedDraw; | 141 fLastDrawWasBuffered = kNo_BufferedDraw; |
145 | 142 |
146 fAARectRenderer = SkNEW(GrAARectRenderer); | 143 fAARectRenderer = SkNEW(GrAARectRenderer); |
147 fOvalRenderer = SkNEW(GrOvalRenderer); | 144 fOvalRenderer = SkNEW(GrOvalRenderer); |
148 | 145 |
149 fDidTestPMConversions = false; | 146 fDidTestPMConversions = false; |
150 | 147 |
151 this->setupDrawBuffer(); | 148 this->setupDrawBuffer(); |
152 | 149 |
153 return true; | 150 return true; |
154 } | 151 } |
155 | 152 |
156 GrContext::~GrContext() { | 153 GrContext::~GrContext() { |
157 if (NULL == fGpu) { | 154 if (NULL == fGpu) { |
158 return; | 155 return; |
159 } | 156 } |
160 | 157 |
161 this->flush(); | 158 this->flush(); |
162 | 159 |
163 for (int i = 0; i < fCleanUpData.count(); ++i) { | 160 for (int i = 0; i < fCleanUpData.count(); ++i) { |
164 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo); | 161 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo); |
165 } | 162 } |
166 | 163 |
167 delete fResourceCache2; | 164 // Since the gpu can hold scratch textures, give it a chance to let go |
168 fResourceCache2 = NULL; | 165 // of them before freeing the texture cache |
| 166 fGpu->purgeResources(); |
| 167 |
169 delete fResourceCache; | 168 delete fResourceCache; |
170 fResourceCache = NULL; | 169 fResourceCache = NULL; |
171 delete fFontCache; | 170 delete fFontCache; |
172 delete fDrawBuffer; | 171 delete fDrawBuffer; |
173 delete fDrawBufferVBAllocPool; | 172 delete fDrawBufferVBAllocPool; |
174 delete fDrawBufferIBAllocPool; | 173 delete fDrawBufferIBAllocPool; |
175 | 174 |
176 fAARectRenderer->unref(); | 175 fAARectRenderer->unref(); |
177 fOvalRenderer->unref(); | 176 fOvalRenderer->unref(); |
178 | 177 |
179 fGpu->unref(); | 178 fGpu->unref(); |
180 SkSafeUnref(fPathRendererChain); | 179 SkSafeUnref(fPathRendererChain); |
181 SkSafeUnref(fSoftwarePathRenderer); | 180 SkSafeUnref(fSoftwarePathRenderer); |
182 fDrawState->unref(); | 181 fDrawState->unref(); |
183 } | 182 } |
184 | 183 |
185 void GrContext::abandonContext() { | 184 void GrContext::abandonContext() { |
186 // abandon first to so destructors | 185 // abandon first to so destructors |
187 // don't try to free the resources in the API. | 186 // don't try to free the resources in the API. |
188 fResourceCache2->abandonAll(); | 187 fGpu->abandonResources(); |
189 | |
190 fGpu->contextAbandonded(); | |
191 | 188 |
192 // a path renderer may be holding onto resources that | 189 // a path renderer may be holding onto resources that |
193 // are now unusable | 190 // are now unusable |
194 SkSafeSetNull(fPathRendererChain); | 191 SkSafeSetNull(fPathRendererChain); |
195 SkSafeSetNull(fSoftwarePathRenderer); | 192 SkSafeSetNull(fSoftwarePathRenderer); |
196 | 193 |
197 delete fDrawBuffer; | 194 delete fDrawBuffer; |
198 fDrawBuffer = NULL; | 195 fDrawBuffer = NULL; |
199 | 196 |
200 delete fDrawBufferVBAllocPool; | 197 delete fDrawBufferVBAllocPool; |
201 fDrawBufferVBAllocPool = NULL; | 198 fDrawBufferVBAllocPool = NULL; |
202 | 199 |
203 delete fDrawBufferIBAllocPool; | 200 delete fDrawBufferIBAllocPool; |
204 fDrawBufferIBAllocPool = NULL; | 201 fDrawBufferIBAllocPool = NULL; |
205 | 202 |
206 fAARectRenderer->reset(); | 203 fAARectRenderer->reset(); |
207 fOvalRenderer->reset(); | 204 fOvalRenderer->reset(); |
208 | 205 |
209 fResourceCache->purgeAllUnlocked(); | 206 fResourceCache->purgeAllUnlocked(); |
210 | 207 |
211 fFontCache->freeAll(); | 208 fFontCache->freeAll(); |
212 fLayerCache->freeAll(); | 209 fLayerCache->freeAll(); |
| 210 fGpu->markContextDirty(); |
213 } | 211 } |
214 | 212 |
215 void GrContext::resetContext(uint32_t state) { | 213 void GrContext::resetContext(uint32_t state) { |
216 fGpu->markContextDirty(state); | 214 fGpu->markContextDirty(state); |
217 } | 215 } |
218 | 216 |
219 void GrContext::freeGpuResources() { | 217 void GrContext::freeGpuResources() { |
220 this->flush(); | 218 this->flush(); |
221 | 219 |
222 fGpu->purgeResources(); | 220 fGpu->purgeResources(); |
223 if (NULL != fDrawBuffer) { | |
224 fDrawBuffer->purgeResources(); | |
225 } | |
226 | 221 |
227 fAARectRenderer->reset(); | 222 fAARectRenderer->reset(); |
228 fOvalRenderer->reset(); | 223 fOvalRenderer->reset(); |
229 | 224 |
230 fResourceCache->purgeAllUnlocked(); | 225 fResourceCache->purgeAllUnlocked(); |
231 fFontCache->freeAll(); | 226 fFontCache->freeAll(); |
232 fLayerCache->freeAll(); | 227 fLayerCache->freeAll(); |
233 // a path renderer may be holding onto resources | 228 // a path renderer may be holding onto resources |
234 SkSafeSetNull(fPathRendererChain); | 229 SkSafeSetNull(fPathRendererChain); |
235 SkSafeSetNull(fSoftwarePathRenderer); | 230 SkSafeSetNull(fSoftwarePathRenderer); |
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
538 this->purgeCache(); | 533 this->purgeCache(); |
539 } else { | 534 } else { |
540 // When we aren't reusing textures we know this scratch texture | 535 // When we aren't reusing textures we know this scratch texture |
541 // will never be reused and would be just wasting time in the cache | 536 // will never be reused and would be just wasting time in the cache |
542 fResourceCache->makeNonExclusive(texture->getCacheEntry()); | 537 fResourceCache->makeNonExclusive(texture->getCacheEntry()); |
543 fResourceCache->deleteResource(texture->getCacheEntry()); | 538 fResourceCache->deleteResource(texture->getCacheEntry()); |
544 } | 539 } |
545 } | 540 } |
546 | 541 |
547 void GrContext::unlockScratchTexture(GrTexture* texture) { | 542 void GrContext::unlockScratchTexture(GrTexture* texture) { |
548 if (texture->wasDestroyed()) { | |
549 return; | |
550 } | |
551 | |
552 ASSERT_OWNED_RESOURCE(texture); | 543 ASSERT_OWNED_RESOURCE(texture); |
553 SkASSERT(NULL != texture->getCacheEntry()); | 544 SkASSERT(NULL != texture->getCacheEntry()); |
554 | 545 |
555 // If this is a scratch texture we detached it from the cache | 546 // If this is a scratch texture we detached it from the cache |
556 // while it was locked (to avoid two callers simultaneously getting | 547 // while it was locked (to avoid two callers simultaneously getting |
557 // the same texture). | 548 // the same texture). |
558 if (texture->getCacheEntry()->key().isScratch()) { | 549 if (texture->getCacheEntry()->key().isScratch()) { |
559 if (fGpu->caps()->reuseScratchTextures() || NULL != texture->asRenderTar
get()) { | 550 if (fGpu->caps()->reuseScratchTextures() || NULL != texture->asRenderTar
get()) { |
560 fResourceCache->makeNonExclusive(texture->getCacheEntry()); | 551 fResourceCache->makeNonExclusive(texture->getCacheEntry()); |
561 this->purgeCache(); | 552 this->purgeCache(); |
(...skipping 1378 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1940 fDrawBuffer->removeGpuTraceMarker(marker); | 1931 fDrawBuffer->removeGpuTraceMarker(marker); |
1941 } | 1932 } |
1942 } | 1933 } |
1943 | 1934 |
1944 /////////////////////////////////////////////////////////////////////////////// | 1935 /////////////////////////////////////////////////////////////////////////////// |
1945 #if GR_CACHE_STATS | 1936 #if GR_CACHE_STATS |
1946 void GrContext::printCacheStats() const { | 1937 void GrContext::printCacheStats() const { |
1947 fResourceCache->printStats(); | 1938 fResourceCache->printStats(); |
1948 } | 1939 } |
1949 #endif | 1940 #endif |
OLD | NEW |