CompositingRenderTargetOGL.h (7763B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef MOZILLA_GFX_COMPOSITINGRENDERTARGETOGL_H 8 #define MOZILLA_GFX_COMPOSITINGRENDERTARGETOGL_H 9 10 #include "GLContextTypes.h" // for GLContext 11 #include "GLDefs.h" // for GLenum, LOCAL_GL_FRAMEBUFFER, etc 12 #include "mozilla/Assertions.h" // for MOZ_ASSERT, etc 13 #include "mozilla/RefPtr.h" // for RefPtr, already_AddRefed 14 #include "mozilla/gfx/Point.h" // for IntSize, IntSizeTyped 15 #include "mozilla/gfx/Types.h" // for SurfaceFormat, etc 16 #include "mozilla/layers/Compositor.h" // for SurfaceInitMode, etc 17 #include "mozilla/layers/TextureHost.h" // for CompositingRenderTarget 18 #include "mozilla/layers/CompositorOGL.h" // for CompositorOGL 19 #include "mozilla/mozalloc.h" // for operator new 20 #include "nsAString.h" 21 #include "nsCOMPtr.h" // for already_AddRefed 22 #include "nsDebug.h" // for NS_ERROR, NS_WARNING 23 #include "nsString.h" // for nsAutoCString 24 25 namespace mozilla { 26 namespace gl { 27 class BindableTexture; 28 } // namespace gl 29 namespace gfx { 30 class DataSourceSurface; 31 } // namespace gfx 32 33 namespace layers { 34 35 class TextureSource; 36 37 class CompositingRenderTargetOGL : public CompositingRenderTarget { 38 typedef mozilla::gl::GLContext GLContext; 39 40 friend class CompositorOGL; 41 42 enum class GLResourceOwnership : uint8_t { 43 // Framebuffer and texture will be deleted when the RenderTarget is 44 // destroyed. 45 OWNED_BY_RENDER_TARGET, 46 47 // Framebuffer and texture are only used by the RenderTarget, but never 48 // deleted. 49 EXTERNALLY_OWNED 50 }; 51 52 struct InitParams { 53 GLenum mFBOTextureTarget; 54 SurfaceInitMode mInitMode; 55 }; 56 57 public: 58 ~CompositingRenderTargetOGL(); 59 60 const char* Name() const override { return "CompositingRenderTargetOGL"; } 61 62 /** 63 * Create a render target around the default FBO, for rendering straight to 64 * the window. 65 */ 66 static already_AddRefed<CompositingRenderTargetOGL> CreateForWindow( 67 CompositorOGL* aCompositor, const gfx::IntSize& aSize) { 68 RefPtr<CompositingRenderTargetOGL> result = new CompositingRenderTargetOGL( 69 aCompositor, gfx::IntRect(gfx::IntPoint(), aSize), gfx::IntPoint(), 70 aSize, GLResourceOwnership::EXTERNALLY_OWNED, 0, 0, Nothing()); 71 return result.forget(); 72 } 73 74 static already_AddRefed<CompositingRenderTargetOGL> 75 CreateForNewFBOAndTakeOwnership(CompositorOGL* aCompositor, GLuint aTexture, 76 GLuint aFBO, const gfx::IntRect& aRect, 77 const gfx::IntPoint& aClipSpaceOrigin, 78 const gfx::IntSize& aPhySize, 79 GLenum aFBOTextureTarget, 80 SurfaceInitMode aInit) { 81 RefPtr<CompositingRenderTargetOGL> result = new CompositingRenderTargetOGL( 82 aCompositor, aRect, aClipSpaceOrigin, aPhySize, 83 GLResourceOwnership::OWNED_BY_RENDER_TARGET, aTexture, aFBO, 84 Some(InitParams{aFBOTextureTarget, aInit})); 85 return result.forget(); 86 } 87 88 static already_AddRefed<CompositingRenderTargetOGL> 89 CreateForExternallyOwnedFBO(CompositorOGL* aCompositor, GLuint aFBO, 90 const gfx::IntRect& aRect, 91 const gfx::IntPoint& aClipSpaceOrigin) { 92 RefPtr<CompositingRenderTargetOGL> result = new CompositingRenderTargetOGL( 93 aCompositor, aRect, aClipSpaceOrigin, aRect.Size(), 94 GLResourceOwnership::EXTERNALLY_OWNED, 0, aFBO, Nothing()); 95 return result.forget(); 96 } 97 98 void BindTexture(GLenum aTextureUnit, GLenum aTextureTarget); 99 100 /** 101 * Call when we want to draw into our FBO 102 */ 103 void BindRenderTarget(); 104 105 bool IsWindow() { return mFBO == 0; } 106 107 GLuint GetFBO() const; 108 109 GLuint GetTextureHandle() const { 110 MOZ_ASSERT(!mNeedInitialization); 111 return mTextureHandle; 112 } 113 114 // TextureSourceOGL 115 TextureSourceOGL* AsSourceOGL() override { 116 // XXX - Bug 900770 117 MOZ_ASSERT( 118 false, 119 "CompositingRenderTargetOGL should not be used as a TextureSource"); 120 return nullptr; 121 } 122 gfx::IntSize GetSize() const override { return mSize; } 123 124 // The point that DrawGeometry's aClipRect is relative to. Will be (0, 0) for 125 // root render targets and equal to GetOrigin() for non-root render targets. 126 gfx::IntPoint GetClipSpaceOrigin() const { return mClipSpaceOrigin; } 127 128 gfx::SurfaceFormat GetFormat() const override { 129 // XXX - Should it be implemented ? is the above assert true ? 130 MOZ_ASSERT(false, "Not implemented"); 131 return gfx::SurfaceFormat::UNKNOWN; 132 } 133 134 // In render target coordinates, i.e. the same space as GetOrigin(). 135 // NOT relative to mClipSpaceOrigin! 136 void SetClipRect(const Maybe<gfx::IntRect>& aRect) { mClipRect = aRect; } 137 const Maybe<gfx::IntRect>& GetClipRect() const { return mClipRect; } 138 139 #ifdef MOZ_DUMP_PAINTING 140 already_AddRefed<gfx::DataSourceSurface> Dump( 141 Compositor* aCompositor) override; 142 #endif 143 144 const gfx::IntSize& GetInitSize() const { return mSize; } 145 const gfx::IntSize& GetPhysicalSize() const { return mPhySize; } 146 147 protected: 148 CompositingRenderTargetOGL(CompositorOGL* aCompositor, 149 const gfx::IntRect& aRect, 150 const gfx::IntPoint& aClipSpaceOrigin, 151 const gfx::IntSize& aPhySize, 152 GLResourceOwnership aGLResourceOwnership, 153 GLuint aTexure, GLuint aFBO, 154 const Maybe<InitParams>& aNeedInitialization) 155 : CompositingRenderTarget(aRect.TopLeft()), 156 mNeedInitialization(aNeedInitialization), 157 mSize(aRect.Size()), 158 mPhySize(aPhySize), 159 mCompositor(aCompositor), 160 mGL(aCompositor->gl()), 161 mClipSpaceOrigin(aClipSpaceOrigin), 162 mGLResourceOwnership(aGLResourceOwnership), 163 mTextureHandle(aTexure), 164 mFBO(aFBO) { 165 MOZ_ASSERT(mGL); 166 } 167 168 /** 169 * Actually do the initialisation. 170 * We do this lazily so that when we first set this render target on the 171 * compositor we do not have to re-bind the FBO after unbinding it, or 172 * alternatively leave the FBO bound after creation. Note that we leave our 173 * FBO bound, and so calling this method is only suitable when about to use 174 * this render target. 175 */ 176 void Initialize(GLenum aFBOTextureTarget); 177 178 /** 179 * Some() between construction and Initialize, if initialization was 180 * requested. 181 */ 182 Maybe<InitParams> mNeedInitialization; 183 184 /* 185 * Users of render target would draw in logical size, but it is 186 * actually drawn to a surface in physical size. GL surfaces have 187 * a limitation on their size, a smaller surface would be 188 * allocated for the render target if the caller requests in a 189 * size too big. 190 */ 191 gfx::IntSize mSize; // Logical size, the expected by callers. 192 gfx::IntSize mPhySize; // Physical size, the real size of the surface. 193 194 /** 195 * There is temporary a cycle between the compositor and the render target, 196 * each having a strong ref to the other. The compositor's reference to 197 * the target is always cleared at the end of a frame. 198 */ 199 RefPtr<CompositorOGL> mCompositor; 200 RefPtr<GLContext> mGL; 201 Maybe<gfx::IntRect> mClipRect; 202 gfx::IntPoint mClipSpaceOrigin; 203 GLResourceOwnership mGLResourceOwnership; 204 GLuint mTextureHandle; 205 GLuint mFBO; 206 }; 207 208 } // namespace layers 209 } // namespace mozilla 210 211 #endif /* MOZILLA_GFX_SURFACEOGL_H */