1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef GFX_IMAGELAYEROGL_H
7 #define GFX_IMAGELAYEROGL_H
9 #include "GLContextTypes.h" // for GLContext, GLuint
10 #include "ImageContainer.h" // for ImageBackendData, etc
11 #include "ImageLayers.h" // for ImageLayer
12 #include "LayerManagerOGL.h" // for LayerOGL
13 #include "gfxPoint.h" // for gfxIntSize
14 #include "mozilla/Assertions.h" // for MOZ_ASSERT_HELPER2
15 #include "mozilla/Mutex.h" // for Mutex
16 #include "mozilla/mozalloc.h" // for operator delete
17 #include "nsAutoPtr.h" // for nsRefPtr
18 #include "nsISupportsImpl.h" // for TextureRecycleBin::Release, etc
19 #include "nsTArray.h" // for nsTArray
20 #include "opengl/LayerManagerOGLProgram.h" // for ShaderProgramType, etc
27 class BlobYCbCrSurface
;
31 * This class wraps a GL texture. It includes a GLContext reference
32 * so we can use to free the texture when destroyed. The implementation
33 * makes sure to always free the texture on the main thread, even if the
34 * destructor runs on another thread.
36 * We ensure that the GLContext reference is only addrefed and released
37 * on the main thread, although it uses threadsafe recounting so we don't
40 * Initially the texture is not allocated --- it's in a "null" state.
44 typedef mozilla::gl::GLContext GLContext
;
51 * Allocate the texture. This can only be called on the main thread.
53 void Allocate(GLContext
*aContext
);
55 * Move the state of aOther to this GLTexture. If this GLTexture currently
56 * has a texture, it is released. This can be called on any thread.
58 void TakeFrom(GLTexture
*aOther
);
60 bool IsAllocated() { return mTexture
!= 0; }
61 GLuint
GetTextureID() { return mTexture
; }
62 GLContext
*GetGLContext() { return mContext
; }
67 nsRefPtr
<GLContext
> mContext
;
72 * A RecycleBin is owned by an ImageLayer. We store textures in it that we
73 * want to recycle from one image to the next. It's a separate object from
74 * ImageContainer because images need to store a strong ref to their RecycleBin
75 * and we must avoid creating a reference loop between an ImageContainer and
78 class TextureRecycleBin
{
79 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(TextureRecycleBin
)
81 typedef mozilla::gl::GLContext GLContext
;
91 void RecycleTexture(GLTexture
*aTexture
, TextureType aType
,
92 const gfxIntSize
& aSize
);
93 void GetTexture(TextureType aType
, const gfxIntSize
& aSize
,
94 GLContext
*aContext
, GLTexture
*aOutTexture
);
97 typedef mozilla::Mutex Mutex
;
99 // This protects mRecycledBuffers, mRecycledBufferSize, mRecycledTextures
100 // and mRecycledTextureSizes
103 nsTArray
<GLTexture
> mRecycledTextures
[2];
104 gfxIntSize mRecycledTextureSizes
[2];
107 class ImageLayerOGL
: public ImageLayer
,
111 ImageLayerOGL(LayerManagerOGL
*aManager
);
112 ~ImageLayerOGL() { Destroy(); }
114 // LayerOGL Implementation
115 virtual void Destroy() { mDestroyed
= true; }
116 virtual Layer
* GetLayer();
117 virtual bool LoadAsTexture(GLuint aTextureUnit
, gfxIntSize
* aSize
);
119 virtual void RenderLayer(int aPreviousFrameBuffer
,
120 const nsIntPoint
& aOffset
);
121 virtual void CleanupResources() {}
124 void AllocateTexturesYCbCr(PlanarYCbCrImage
*aImage
);
125 void AllocateTexturesCairo(CairoImage
*aImage
);
128 nsRefPtr
<TextureRecycleBin
> mTextureRecycleBin
;
131 struct PlanarYCbCrOGLBackendData
: public ImageBackendData
133 ~PlanarYCbCrOGLBackendData()
136 mTextureRecycleBin
->RecycleTexture(&mTextures
[0], TextureRecycleBin::TEXTURE_Y
, mYSize
);
137 mTextureRecycleBin
->RecycleTexture(&mTextures
[1], TextureRecycleBin::TEXTURE_C
, mCbCrSize
);
138 mTextureRecycleBin
->RecycleTexture(&mTextures
[2], TextureRecycleBin::TEXTURE_C
, mCbCrSize
);
144 return mTextures
[0].IsAllocated() && mTextures
[1].IsAllocated() &&
145 mTextures
[2].IsAllocated();
148 GLTexture mTextures
[3];
149 gfxIntSize mYSize
, mCbCrSize
;
150 nsRefPtr
<TextureRecycleBin
> mTextureRecycleBin
;
154 struct CairoOGLBackendData
: public ImageBackendData
156 CairoOGLBackendData() : mLayerProgram(RGBALayerProgramType
) {}
158 ShaderProgramType mLayerProgram
;
159 gfxIntSize mTextureSize
;
164 #endif /* GFX_IMAGELAYEROGL_H */