Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(135)

Side by Side Diff: include/gpu/GrEffect.h

Issue 14081016: Remove static effects from the effect memory pool. (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright 2012 Google Inc. 2 * Copyright 2012 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #ifndef GrEffect_DEFINED 8 #ifndef GrEffect_DEFINED
9 #define GrEffect_DEFINED 9 #define GrEffect_DEFINED
10 10
11 #include "GrColor.h" 11 #include "GrColor.h"
12 #include "GrEffectUnitTest.h" 12 #include "GrEffectUnitTest.h"
13 #include "GrNoncopyable.h" 13 #include "GrNoncopyable.h"
14 #include "GrRefCnt.h" 14 #include "GrRefCnt.h"
15 #include "GrTexture.h" 15 #include "GrTexture.h"
16 #include "GrTextureAccess.h" 16 #include "GrTextureAccess.h"
17 #include "GrTypesPriv.h" 17 #include "GrTypesPriv.h"
18 18
19 class GrBackendEffectFactory; 19 class GrBackendEffectFactory;
20 class GrContext; 20 class GrContext;
21 class GrEffect; 21 class GrEffect;
22 class SkString; 22 class SkString;
23 23
24 /** 24 /**
25 * A Wrapper class for GrEffect. Its ref-count will track owners that may use ef fects to enqueue 25 * A Wrapper class for GrEffect. Its ref-count will track owners that may use ef fects to enqueue
26 * new draw operations separately from ownership within a deferred drawing queue . When the 26 * new draw operations separately from ownership within a deferred drawing queue . When the
27 * GrEffectRef ref count reaches zero the scratch GrResources owned by the effec t can be recycled 27 * GrEffectRef ref count reaches zero the scratch GrResources owned by the effec t can be recycled
28 * in service of later draws. However, the deferred draw queue may still own dir ect references to 28 * in service of later draws. However, the deferred draw queue may still own dir ect references to
29 * the underlying GrEffect. 29 * the underlying GrEffect.
30 *
robertphillips 2013/04/22 22:05:13 pools -> pool
31 * GrEffectRefs created by new are placed in a per-thread managed pool. The pool s is destroyed when
32 * the thread ends. Therefore, all dynamically allocated GrEffectRefs must be un reffed before thread
33 * termination.
30 */ 34 */
31 class GrEffectRef : public SkRefCnt { 35 class GrEffectRef : public SkRefCnt {
32 public: 36 public:
33 SK_DECLARE_INST_COUNT(GrEffectRef); 37 SK_DECLARE_INST_COUNT(GrEffectRef);
38 virtual ~GrEffectRef();
34 39
35 GrEffect* get() { return fEffect; } 40 GrEffect* get() { return fEffect; }
36 const GrEffect* get() const { return fEffect; } 41 const GrEffect* get() const { return fEffect; }
37 42
38 const GrEffect* operator-> () { return fEffect; } 43 const GrEffect* operator-> () { return fEffect; }
39 const GrEffect* operator-> () const { return fEffect; } 44 const GrEffect* operator-> () const { return fEffect; }
40 45
41 void* operator new(size_t size); 46 void* operator new(size_t size);
42 void operator delete(void* target); 47 void operator delete(void* target);
43 48
49 void* operator new(size_t size, void* placement) {
50 return ::operator new(size, placement);
51 }
52 void operator delete(void* target, void* placement) {
53 ::operator delete(target, placement);
54 }
55
44 private: 56 private:
45 friend class GrEffect; // to construct these 57 friend class GrEffect; // to construct these
46 58
47 explicit GrEffectRef(GrEffect* effect); 59 explicit GrEffectRef(GrEffect* effect);
48 60
49 virtual ~GrEffectRef();
50
51 GrEffect* fEffect; 61 GrEffect* fEffect;
52 62
53 typedef SkRefCnt INHERITED; 63 typedef SkRefCnt INHERITED;
54 }; 64 };
55 65
56 /** Provides custom vertex shader, fragment shader, uniform data for a particula r stage of the 66 /** Provides custom vertex shader, fragment shader, uniform data for a particula r stage of the
57 Ganesh shading pipeline. 67 Ganesh shading pipeline.
58 Subclasses must have a function that produces a human-readable name: 68 Subclasses must have a function that produces a human-readable name:
59 static const char* Name(); 69 static const char* Name();
60 GrEffect objects *must* be immutable: after being constructed, their fields may not change. 70 GrEffect objects *must* be immutable: after being constructed, their fields may not change.
61 71
62 GrEffect subclass objects should be created by factory functions that return GrEffectRef. 72 GrEffect subclass objects should be created by factory functions that return GrEffectRef.
63 There is no public way to wrap a GrEffect in a GrEffectRef. Thus, a factory should be a static 73 There is no public way to wrap a GrEffect in a GrEffectRef. Thus, a factory should be a static
64 member function of a GrEffect subclass. 74 member function of a GrEffect subclass.
65 75
66 Because almost no code should ever handle a GrEffect outside of a GrEffectRe f, we privately 76 Because almost no code should ever handle a GrEffect directly outside of a G rEffectRef, we
67 inherit from GrRefCnt to help prevent accidental direct ref'ing/unref'ing of effects. 77 privately inherit from GrRefCnt to help prevent accidental direct ref'ing/un ref'ing of effects.
78
79 Dynamically allocated GrEffects and their corresponding GrEffectRefs are man aged by a per-thread
80 memory pool. The ref count of an effect must reach 0 before the thread termi nates and the pool
81 is destroyed. To create a static effect use the macro GR_CREATE_STATIC_EFFEC T declared below.
68 */ 82 */
69 class GrEffect : private GrRefCnt { 83 class GrEffect : private GrRefCnt {
70 public: 84 public:
71 SK_DECLARE_INST_COUNT(GrEffect) 85 SK_DECLARE_INST_COUNT(GrEffect)
72 86
73 /** 87 /**
74 * The types of vertex coordinates available to an effect in the vertex shad er. Effects can 88 * The types of vertex coordinates available to an effect in the vertex shad er. Effects can
75 * require their own vertex attribute but these coordinates are made availab le by the framework 89 * require their own vertex attribute but these coordinates are made availab le by the framework
76 * in all programs. kCustom_CoordsType is provided to signify that an altern ative set of coords 90 * in all programs. kCustom_CoordsType is provided to signify that an altern ative set of coords
77 * is used (usually an explicit vertex attribute) but its meaning is determi ned by the effect 91 * is used (usually an explicit vertex attribute) but its meaning is determi ned by the effect
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 static inline SkMatrix MakeDivByTextureWHMatrix(const GrTexture* texture) { 166 static inline SkMatrix MakeDivByTextureWHMatrix(const GrTexture* texture) {
153 GrAssert(NULL != texture); 167 GrAssert(NULL != texture);
154 SkMatrix mat; 168 SkMatrix mat;
155 mat.setIDiv(texture->width(), texture->height()); 169 mat.setIDiv(texture->width(), texture->height());
156 return mat; 170 return mat;
157 } 171 }
158 172
159 void* operator new(size_t size); 173 void* operator new(size_t size);
160 void operator delete(void* target); 174 void operator delete(void* target);
161 175
176 void* operator new(size_t size, void* placement) {
177 return ::operator new(size, placement);
178 }
179 void operator delete(void* target, void* placement) {
180 ::operator delete(target, placement);
181 }
182
162 /** These functions are used when recording effects into a deferred drawing queue. The inc call 183 /** These functions are used when recording effects into a deferred drawing queue. The inc call
163 keeps the effect alive outside of GrEffectRef while allowing any resourc es owned by the 184 keeps the effect alive outside of GrEffectRef while allowing any resourc es owned by the
164 effect to be returned to the cache for reuse. The dec call must balance the inc call. */ 185 effect to be returned to the cache for reuse. The dec call must balance the inc call. */
165 void incDeferredRefCounts() const { 186 void incDeferredRefCounts() const {
166 this->ref(); 187 this->ref();
167 int count = fTextureAccesses.count(); 188 int count = fTextureAccesses.count();
168 for (int t = 0; t < count; ++t) { 189 for (int t = 0; t < count; ++t) {
169 fTextureAccesses[t]->getTexture()->incDeferredRefCount(); 190 fTextureAccesses[t]->getTexture()->incDeferredRefCount();
170 } 191 }
171 } 192 }
(...skipping 30 matching lines...) Expand all
202 } else { 223 } else {
203 effect->fEffectRef->ref(); 224 effect->fEffectRef->ref();
204 } 225 }
205 return effect->fEffectRef; 226 return effect->fEffectRef;
206 } 227 }
207 228
208 static const GrEffectRef* CreateEffectRef(const GrEffect* effect) { 229 static const GrEffectRef* CreateEffectRef(const GrEffect* effect) {
209 return CreateEffectRef(const_cast<GrEffect*>(effect)); 230 return CreateEffectRef(const_cast<GrEffect*>(effect));
210 } 231 }
211 232
233 /** Used by GR_CREATE_STATIC_EFFECT below */
234 static GrEffectRef* CreateStaticEffectRef(void* refStorage, GrEffect* effect ) {
235 GrAssert(NULL == effect->fEffectRef);
236 effect->fEffectRef = SkNEW_PLACEMENT_ARGS(refStorage, GrEffectRef, (effe ct));
237 return effect->fEffectRef;
238 }
239
240
212 /** Helper used in subclass factory functions to unref the effect after it h as been wrapped in a 241 /** Helper used in subclass factory functions to unref the effect after it h as been wrapped in a
213 GrEffectRef. E.g.: 242 GrEffectRef. E.g.:
214 243
215 class EffectSubclass : public GrEffect { 244 class EffectSubclass : public GrEffect {
216 public: 245 public:
217 GrEffectRef* Create(ParamType1 param1, ParamType2 param2, ...) { 246 GrEffectRef* Create(ParamType1 param1, ParamType2 param2, ...) {
218 AutoEffectUnref effect(SkNEW_ARGS(EffectSubclass, (param1, param 2, ...))); 247 AutoEffectUnref effect(SkNEW_ARGS(EffectSubclass, (param1, param 2, ...)));
219 return CreateEffectRef(effect); 248 return CreateEffectRef(effect);
220 } 249 }
221 */ 250 */
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
278 307
279 typedef GrRefCnt INHERITED; 308 typedef GrRefCnt INHERITED;
280 }; 309 };
281 310
282 inline GrEffectRef::GrEffectRef(GrEffect* effect) { 311 inline GrEffectRef::GrEffectRef(GrEffect* effect) {
283 GrAssert(NULL != effect); 312 GrAssert(NULL != effect);
284 effect->ref(); 313 effect->ref();
285 fEffect = effect; 314 fEffect = effect;
286 } 315 }
287 316
317 /**
318 * This creates an effect outside of the effect memory pool. The effect's destru ctor will be called
319 * at global destruction time. NAME will be the name of the created GrEffectRef.
320 */
321 #define GR_CREATE_STATIC_EFFECT(NAME, EFFECT_CLASS, ARGS) \
322 enum { \
323 k_##NAME##_EffectRefOffset = GR_CT_ALIGN_UP(sizeof(EFFECT_CLASS), 8), \
324 k_##NAME##_StorageSize = k_##NAME##_EffectRefOffset + sizeof(GrEffectRef) \
325 }; \
326 static SkAlignedSStorage<k_##NAME##_StorageSize> g_##NAME##_Storage; \
327 static void* NAME##_RefLocation = (char*)g_##NAME##_Storage.get() + k_##NAME##_E ffectRefOffset; \
328 static GrEffect* NAME##_Effect SkNEW_PLACEMENT_ARGS(g_##NAME##_Storage.get(), EF FECT_CLASS, ARGS);\
329 static SkAutoTDestroy<GrEffect> NAME##_ad(NAME##_Effect); \
330 static GrEffectRef* NAME(GrEffect::CreateStaticEffectRef(NAME##_RefLocation, NAM E##_Effect)); \
331 static SkAutoTDestroy<GrEffectRef> NAME##_Ref_ad(NAME)
332
333
288 #endif 334 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698