9c9ab9931241609e2eeacaee2d55882c2f5a52de
[gecko.git] / ImageLayerOGL.h
blob9c9ab9931241609e2eeacaee2d55882c2f5a52de
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
22 struct nsIntPoint;
24 namespace mozilla {
25 namespace layers {
27 class BlobYCbCrSurface;
28 class Layer;
30 /**
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
38 * really have to.
40 * Initially the texture is not allocated --- it's in a "null" state.
42 class GLTexture
44 typedef mozilla::gl::GLContext GLContext;
46 public:
47 GLTexture();
48 ~GLTexture();
50 /**
51 * Allocate the texture. This can only be called on the main thread.
53 void Allocate(GLContext *aContext);
54 /**
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; }
64 void Release();
65 private:
67 nsRefPtr<GLContext> mContext;
68 GLuint mTexture;
71 /**
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
76 * its active image.
78 class TextureRecycleBin {
79 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(TextureRecycleBin)
81 typedef mozilla::gl::GLContext GLContext;
83 public:
84 TextureRecycleBin();
86 enum TextureType {
87 TEXTURE_Y,
88 TEXTURE_C
91 void RecycleTexture(GLTexture *aTexture, TextureType aType,
92 const gfxIntSize& aSize);
93 void GetTexture(TextureType aType, const gfxIntSize& aSize,
94 GLContext *aContext, GLTexture *aOutTexture);
96 private:
97 typedef mozilla::Mutex Mutex;
99 // This protects mRecycledBuffers, mRecycledBufferSize, mRecycledTextures
100 // and mRecycledTextureSizes
101 Mutex mLock;
103 nsTArray<GLTexture> mRecycledTextures[2];
104 gfxIntSize mRecycledTextureSizes[2];
107 class ImageLayerOGL : public ImageLayer,
108 public LayerOGL
110 public:
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);
127 protected:
128 nsRefPtr<TextureRecycleBin> mTextureRecycleBin;
131 struct PlanarYCbCrOGLBackendData : public ImageBackendData
133 ~PlanarYCbCrOGLBackendData()
135 if (HasTextures()) {
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);
142 bool HasTextures()
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) {}
157 GLTexture mTexture;
158 ShaderProgramType mLayerProgram;
159 gfxIntSize mTextureSize;
162 } /* layers */
163 } /* mozilla */
164 #endif /* GFX_IMAGELAYEROGL_H */