1 : /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : * ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Mozilla Corporation code.
16 : *
17 : * The Initial Developer of the Original Code is Mozilla Foundation.
18 : * Portions created by the Initial Developer are Copyright (C) 2009
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Bas Schouten <bschouten@mozilla.org>
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #ifndef GFX_IMAGELAYEROGL_H
39 : #define GFX_IMAGELAYEROGL_H
40 :
41 : #include "mozilla/layers/PLayers.h"
42 : #include "mozilla/layers/ShadowLayers.h"
43 :
44 : #include "LayerManagerOGL.h"
45 : #include "ImageLayers.h"
46 : #include "yuv_convert.h"
47 : #include "mozilla/Mutex.h"
48 :
49 : namespace mozilla {
50 : namespace layers {
51 :
52 : /**
53 : * This class wraps a GL texture. It includes a GLContext reference
54 : * so we can use to free the texture when destroyed. The implementation
55 : * makes sure to always free the texture on the main thread, even if the
56 : * destructor runs on another thread.
57 : *
58 : * We ensure that the GLContext reference is only addrefed and released
59 : * on the main thread, although it uses threadsafe recounting so we don't
60 : * really have to.
61 : *
62 : * Initially the texture is not allocated --- it's in a "null" state.
63 : */
64 : class GLTexture {
65 : typedef mozilla::gl::GLContext GLContext;
66 :
67 : public:
68 0 : GLTexture() : mTexture(0) {}
69 0 : ~GLTexture() { Release(); }
70 :
71 : /**
72 : * Allocate the texture. This can only be called on the main thread.
73 : */
74 : void Allocate(GLContext *aContext);
75 : /**
76 : * Move the state of aOther to this GLTexture. If this GLTexture currently
77 : * has a texture, it is released. This can be called on any thread.
78 : */
79 : void TakeFrom(GLTexture *aOther);
80 :
81 0 : bool IsAllocated() { return mTexture != 0; }
82 0 : GLuint GetTextureID() { return mTexture; }
83 0 : GLContext *GetGLContext() { return mContext; }
84 :
85 : private:
86 : void Release();
87 :
88 : nsRefPtr<GLContext> mContext;
89 : GLuint mTexture;
90 : };
91 :
92 : /**
93 : * A RecycleBin is owned by an ImageLayer. We store textures in it that we
94 : * want to recycle from one image to the next. It's a separate object from
95 : * ImageContainer because images need to store a strong ref to their RecycleBin
96 : * and we must avoid creating a reference loop between an ImageContainer and
97 : * its active image.
98 : */
99 0 : class TextureRecycleBin {
100 0 : NS_INLINE_DECL_THREADSAFE_REFCOUNTING(TextureRecycleBin)
101 :
102 : typedef mozilla::gl::GLContext GLContext;
103 :
104 : public:
105 : TextureRecycleBin();
106 :
107 : enum TextureType {
108 : TEXTURE_Y,
109 : TEXTURE_C
110 : };
111 :
112 : void RecycleTexture(GLTexture *aTexture, TextureType aType,
113 : const gfxIntSize& aSize);
114 : void GetTexture(TextureType aType, const gfxIntSize& aSize,
115 : GLContext *aContext, GLTexture *aOutTexture);
116 :
117 : private:
118 : typedef mozilla::Mutex Mutex;
119 :
120 : // This protects mRecycledBuffers, mRecycledBufferSize, mRecycledTextures
121 : // and mRecycledTextureSizes
122 : Mutex mLock;
123 :
124 : nsTArray<GLTexture> mRecycledTextures[2];
125 : gfxIntSize mRecycledTextureSizes[2];
126 : };
127 :
128 : class THEBES_API ImageLayerOGL : public ImageLayer,
129 : public LayerOGL
130 : {
131 : public:
132 : ImageLayerOGL(LayerManagerOGL *aManager);
133 0 : ~ImageLayerOGL() { Destroy(); }
134 :
135 : // LayerOGL Implementation
136 0 : virtual void Destroy() { mDestroyed = true; }
137 : virtual Layer* GetLayer();
138 :
139 : virtual void RenderLayer(int aPreviousFrameBuffer,
140 : const nsIntPoint& aOffset);
141 0 : virtual void CleanupResources() {}
142 :
143 : void AllocateTexturesYCbCr(PlanarYCbCrImage *aImage);
144 : void AllocateTexturesCairo(CairoImage *aImage);
145 :
146 : protected:
147 : nsRefPtr<TextureRecycleBin> mTextureRecycleBin;
148 : };
149 :
150 : struct THEBES_API PlanarYCbCrOGLBackendData : public ImageBackendData
151 0 : {
152 0 : ~PlanarYCbCrOGLBackendData()
153 0 : {
154 0 : if (HasTextures()) {
155 0 : mTextureRecycleBin->RecycleTexture(&mTextures[0], TextureRecycleBin::TEXTURE_Y, mYSize);
156 0 : mTextureRecycleBin->RecycleTexture(&mTextures[1], TextureRecycleBin::TEXTURE_C, mCbCrSize);
157 0 : mTextureRecycleBin->RecycleTexture(&mTextures[2], TextureRecycleBin::TEXTURE_C, mCbCrSize);
158 : }
159 0 : }
160 :
161 0 : bool HasTextures()
162 : {
163 0 : return mTextures[0].IsAllocated() && mTextures[1].IsAllocated() &&
164 0 : mTextures[2].IsAllocated();
165 : }
166 :
167 : GLTexture mTextures[3];
168 : gfxIntSize mYSize, mCbCrSize;
169 : nsRefPtr<TextureRecycleBin> mTextureRecycleBin;
170 : };
171 :
172 :
173 : struct CairoOGLBackendData : public ImageBackendData
174 0 : {
175 0 : CairoOGLBackendData() : mLayerProgram(gl::RGBALayerProgramType), mTiling(false) {}
176 : void SetTiling(bool aTiling);
177 : GLTexture mTexture;
178 : gl::ShaderProgramType mLayerProgram;
179 : bool mTiling;
180 : };
181 :
182 : class ShadowImageLayerOGL : public ShadowImageLayer,
183 : public LayerOGL
184 : {
185 : typedef gl::TextureImage TextureImage;
186 :
187 : public:
188 : ShadowImageLayerOGL(LayerManagerOGL* aManager);
189 : virtual ~ShadowImageLayerOGL();
190 :
191 : // ShadowImageLayer impl
192 : virtual void Swap(const SharedImage& aFront,
193 : SharedImage* aNewBack);
194 :
195 : virtual void Disconnect();
196 :
197 : // LayerOGL impl
198 : virtual void Destroy();
199 :
200 : virtual Layer* GetLayer();
201 :
202 : virtual void RenderLayer(int aPreviousFrameBuffer,
203 : const nsIntPoint& aOffset);
204 :
205 : virtual void CleanupResources();
206 :
207 : private:
208 : bool Init(const SharedImage& aFront);
209 :
210 : nsRefPtr<TextureImage> mTexImage;
211 : GLTexture mYUVTexture[3];
212 : gfxIntSize mSize;
213 : gfxIntSize mCbCrSize;
214 : nsIntRect mPictureRect;
215 : };
216 :
217 : } /* layers */
218 : } /* mozilla */
219 : #endif /* GFX_IMAGELAYEROGL_H */
|