Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 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 | 8 |
| 9 #ifndef GrGLRenderTarget_DEFINED | 9 #ifndef GrGLRenderTarget_DEFINED |
| 10 #define GrGLRenderTarget_DEFINED | 10 #define GrGLRenderTarget_DEFINED |
| 11 | 11 |
| 12 #include "GrGLIRect.h" | 12 #include "GrGLIRect.h" |
| 13 #include "GrRenderTarget.h" | 13 #include "GrRenderTarget.h" |
| 14 #include "SkScalar.h" | 14 #include "SkScalar.h" |
| 15 | 15 |
| 16 class GrGLGpu; | 16 class GrGLGpu; |
| 17 | 17 |
| 18 /** Represents a GL FBO object. It has a gen ID which is valid whenever the FBO ID owned by the | |
| 19 object is valid. The gen IDs are not recycled after FBOs are freed, unlike F BO IDs, and so | |
| 20 can be used to uniquely identity FBO ID instantiations. If this object owns an FBO ID, the ID | |
| 21 must be deleted or abandoned before this object is freed. FBO IDs should nev er be owned by | |
|
egdaniel
2015/02/25 16:21:34
"FBO IDs should never be owned by more that one in
| |
| 22 more than one instance. */ | |
| 23 class GrGLFBO : public SkRefCnt { | |
| 24 public: | |
| 25 SK_DECLARE_INST_COUNT(GrGLFBO); | |
| 26 | |
| 27 /** Initializes to invalid state */ | |
| 28 GrGLFBO() : fGenID(SK_InvalidGenID) {} | |
| 29 | |
| 30 /** Initializes to an FBO. */ | |
| 31 GrGLFBO(GrGLint id) : fID(id), fGenID(NextGenID()) {} | |
| 32 | |
| 33 /** Initializes to an FBO. */ | |
| 34 GrGLFBO(const GrGLInterface* gl) { | |
| 35 GR_GL_CALL(gl, GenFramebuffers(1, &fID)); | |
| 36 if (0 != fID) { | |
| 37 fGenID = NextGenID(); | |
| 38 } else { | |
| 39 fGenID = SK_InvalidGenID; | |
| 40 } | |
| 41 } | |
| 42 | |
| 43 ~GrGLFBO() SK_OVERRIDE { SkASSERT(!this->isValid()); } | |
| 44 | |
| 45 /** If this does not own a FBO ID, attempt to create one. Otherwise, no-op. */ | |
| 46 void generateIfInvalid(const GrGLInterface* gl) { | |
|
egdaniel
2015/02/25 16:21:34
I wonder if this should be public. I feel like it
| |
| 47 if (!this->isValid()) { | |
| 48 GR_GL_CALL(gl, GenFramebuffers(1, &fID)); | |
| 49 if (fID) { | |
| 50 fGenID = NextGenID(); | |
| 51 } | |
| 52 } | |
| 53 } | |
| 54 | |
| 55 /** Does this object own an FBO ID? */ | |
| 56 bool isValid() const { return SK_InvalidGenID != fGenID; } | |
| 57 | |
| 58 uint32_t genID() const { return fGenID; } | |
| 59 | |
| 60 GrGLint fboID() const { SkASSERT(this->isValid()); return fID; } | |
| 61 | |
| 62 bool isDefaultFramebuffer() const { SkASSERT(this->isValid()); return 0 == f ID; } | |
| 63 | |
| 64 /** Give up ownership of the FBO ID owned by this object without deleting it . */ | |
| 65 void abandon(GrGLGpu* gpu); | |
| 66 | |
| 67 /** Delete and give up ownership of the the FBO ID if it is valid. */ | |
| 68 void deleteIfValid(GrGLGpu* gpu); | |
| 69 | |
| 70 private: | |
| 71 static uint32_t NextGenID() { | |
| 72 static int32_t gGenID = SK_InvalidGenID + 1; | |
| 73 return static_cast<uint32_t>(sk_atomic_inc(&gGenID)); | |
| 74 } | |
| 75 | |
| 76 GrGLuint fID; | |
| 77 uint32_t fGenID; | |
| 78 | |
| 79 typedef SkRefCnt INHERITED; | |
| 80 }; | |
| 81 | |
| 82 ////////////////////////////////////////////////////////////////////////////// | |
| 83 | |
| 84 /** GL-specific subclass of GrRenderTarget. */ | |
| 18 class GrGLRenderTarget : public GrRenderTarget { | 85 class GrGLRenderTarget : public GrRenderTarget { |
| 19 public: | 86 public: |
| 20 // set fTexFBOID to this value to indicate that it is multisampled but | |
| 21 // Gr doesn't know how to resolve it. | |
| 22 enum { kUnresolvableFBOID = 0 }; | |
| 23 | |
| 24 struct IDDesc { | 87 struct IDDesc { |
| 25 GrGLuint fRTFBOID; | 88 SkAutoTUnref<GrGLFBO> fRenderFBO; |
| 26 GrGLuint fTexFBOID; | 89 SkAutoTUnref<GrGLFBO> fTextureFBO; |
| 27 GrGLuint fMSColorRenderbufferID; | 90 GrGLuint fMSColorRenderbufferID; |
| 28 GrGpuResource::LifeCycle fLifeCycle; | 91 GrGpuResource::LifeCycle fLifeCycle; |
| 29 }; | 92 }; |
| 30 | 93 |
| 31 GrGLRenderTarget(GrGLGpu*, const GrSurfaceDesc&, const IDDesc&); | 94 GrGLRenderTarget(GrGLGpu*, const GrSurfaceDesc&, const IDDesc&); |
| 32 | 95 |
| 33 void setViewport(const GrGLIRect& rect) { fViewport = rect; } | 96 void setViewport(const GrGLIRect& rect) { fViewport = rect; } |
| 34 const GrGLIRect& getViewport() const { return fViewport; } | 97 const GrGLIRect& getViewport() const { return fViewport; } |
| 35 | 98 |
| 36 // The following two functions return the same ID when a | 99 // For multisampled renderbuffer render targets, these will return different GrGLFBO objects. If |
| 37 // texture/render target is multisampled, and different IDs when | 100 // the render target is not texturable, textureFBO() returns NULL. If the re nder target auto |
| 38 // it is. | 101 // resolves to a texture, the same object is returned. |
| 39 // FBO ID used to render into | 102 |
| 40 GrGLuint renderFBOID() const { return fRTFBOID; } | 103 // FBO that should be rendered into. Always non-NULL. |
| 41 // FBO ID that has texture ID attached. | 104 const GrGLFBO* renderFBO() const { |
| 42 GrGLuint textureFBOID() const { return fTexFBOID; } | 105 SkASSERT(fRenderFBO && fRenderFBO->isValid()); |
| 106 return fRenderFBO; | |
| 107 } | |
| 108 | |
| 109 // FBO that has the target's texture ID attached. The return value may be: | |
| 110 // * NULL when this render target is not a texture, | |
| 111 // * the same as renderFBO() when this surface is not multisampled or a uto-resolves, | |
| 112 // * or different than renderFBO() when it requires explicit resolving via | |
| 113 // glBlitFramebuffer. | |
| 114 const GrGLFBO* textureFBO() const { | |
| 115 SkASSERT(!fTextureFBO || fTextureFBO->isValid()); | |
| 116 return fTextureFBO; | |
| 117 } | |
| 43 | 118 |
| 44 // override of GrRenderTarget | 119 // override of GrRenderTarget |
| 45 ResolveType getResolveType() const SK_OVERRIDE { | 120 ResolveType getResolveType() const SK_OVERRIDE { |
| 46 if (!this->isMultisampled() || | 121 if (!this->isMultisampled() || this->renderFBO() == this->textureFBO()) { |
| 47 fRTFBOID == fTexFBOID) { | |
| 48 // catches FBO 0 and non MSAA case | 122 // catches FBO 0 and non MSAA case |
| 49 return kAutoResolves_ResolveType; | 123 return kAutoResolves_ResolveType; |
| 50 } else if (kUnresolvableFBOID == fTexFBOID) { | 124 } else if (!this->textureFBO()) { |
| 51 return kCantResolve_ResolveType; | 125 return kCantResolve_ResolveType; |
| 52 } else { | 126 } else { |
| 53 return kCanResolve_ResolveType; | 127 return kCanResolve_ResolveType; |
| 54 } | 128 } |
| 55 } | 129 } |
| 56 | 130 |
| 57 /** When we don't own the FBO ID we don't attempt to modify its attachments. */ | 131 /** When we don't own the FBO ID we don't attempt to modify its attachments. */ |
| 58 bool canAttemptStencilAttachment() const SK_OVERRIDE { return !fIsWrapped; } | 132 bool canAttemptStencilAttachment() const SK_OVERRIDE { return !fIsWrapped; } |
| 59 | 133 |
| 60 protected: | 134 protected: |
| 61 // The public constructor registers this object with the cache. However, onl y the most derived | 135 // The public constructor registers this object with the cache. However, onl y the most derived |
| 62 // class should register with the cache. This constructor does not do the re gistration and | 136 // class should register with the cache. This constructor does not do the re gistration and |
| 63 // rather moves that burden onto the derived class. | 137 // rather moves that burden onto the derived class. |
| 64 enum Derived { kDerived }; | 138 enum Derived { kDerived }; |
| 65 GrGLRenderTarget(GrGLGpu*, const GrSurfaceDesc&, const IDDesc&, Derived); | 139 GrGLRenderTarget(GrGLGpu*, const GrSurfaceDesc&, const IDDesc&, Derived); |
| 66 | 140 |
| 67 void init(const GrSurfaceDesc&, const IDDesc&); | 141 void init(const GrSurfaceDesc&, const IDDesc&); |
| 68 | 142 |
| 69 void onAbandon() SK_OVERRIDE; | 143 void onAbandon() SK_OVERRIDE; |
| 70 void onRelease() SK_OVERRIDE; | 144 void onRelease() SK_OVERRIDE; |
| 71 | 145 |
| 72 // In protected because subclass GrGLTextureRenderTarget calls this version. | 146 // In protected because subclass GrGLTextureRenderTarget calls this version. |
| 73 size_t onGpuMemorySize() const SK_OVERRIDE; | 147 size_t onGpuMemorySize() const SK_OVERRIDE; |
| 74 | 148 |
| 75 private: | 149 private: |
| 76 GrGLuint fRTFBOID; | 150 SkAutoTUnref<GrGLFBO> fRenderFBO; |
| 77 GrGLuint fTexFBOID; | 151 SkAutoTUnref<GrGLFBO> fTextureFBO; |
| 78 GrGLuint fMSColorRenderbufferID; | 152 GrGLuint fMSColorRenderbufferID; |
| 79 | 153 |
| 80 // We track this separately from GrGpuResource because this may be both a te xture and a render | 154 // We track this separately from GrGpuResource because this may be both a te xture and a render |
| 81 // target, and the texture may be wrapped while the render target is not. | 155 // target, and the texture may be wrapped while the render target is not. |
| 82 bool fIsWrapped; | 156 bool fIsWrapped; |
| 83 | 157 |
| 84 // when we switch to this render target we want to set the viewport to | 158 // when we switch to this render target we want to set the viewport to |
| 85 // only render to content area (as opposed to the whole allocation) and | 159 // only render to content area (as opposed to the whole allocation) and |
| 86 // we want the rendering to be at top left (GL has origin in bottom left) | 160 // we want the rendering to be at top left (GL has origin in bottom left) |
| 87 GrGLIRect fViewport; | 161 GrGLIRect fViewport; |
| 88 | 162 |
| 89 // onGpuMemorySize() needs to know what how many color values are owned per pixel. However, | 163 // onGpuMemorySize() needs to know what how many color values are owned per pixel. However, |
| 90 // abandon and release zero out the IDs and the cache needs to know the size even after those | 164 // abandon and release zero out the IDs and the cache needs to know the size even after those |
| 91 // actions. | 165 // actions. |
| 92 uint8_t fColorValuesPerPixel; | 166 uint8_t fColorValuesPerPixel; |
| 93 | 167 |
| 94 typedef GrRenderTarget INHERITED; | 168 typedef GrRenderTarget INHERITED; |
| 95 }; | 169 }; |
| 96 | 170 |
| 97 #endif | 171 #endif |
| OLD | NEW |