Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40; -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef SURFACESTREAM_H_ |
michael@0 | 7 | #define SURFACESTREAM_H_ |
michael@0 | 8 | |
michael@0 | 9 | #include <stack> |
michael@0 | 10 | #include <set> |
michael@0 | 11 | #include "mozilla/Monitor.h" |
michael@0 | 12 | #include "mozilla/Attributes.h" |
michael@0 | 13 | #include "mozilla/gfx/Point.h" |
michael@0 | 14 | #include "mozilla/GenericRefCounted.h" |
michael@0 | 15 | #include "SurfaceTypes.h" |
michael@0 | 16 | |
michael@0 | 17 | namespace mozilla { |
michael@0 | 18 | |
michael@0 | 19 | namespace gl { |
michael@0 | 20 | class GLContext; |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | namespace gfx { |
michael@0 | 24 | class SharedSurface; |
michael@0 | 25 | class SurfaceFactory; |
michael@0 | 26 | |
michael@0 | 27 | // Owned by: ScreenBuffer |
michael@0 | 28 | class SurfaceStream : public GenericAtomicRefCounted |
michael@0 | 29 | { |
michael@0 | 30 | public: |
michael@0 | 31 | MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(SurfaceStream) |
michael@0 | 32 | typedef enum { |
michael@0 | 33 | MainThread, |
michael@0 | 34 | OffMainThread |
michael@0 | 35 | } OMTC; |
michael@0 | 36 | |
michael@0 | 37 | static SurfaceStreamType ChooseGLStreamType(OMTC omtc, |
michael@0 | 38 | bool preserveBuffer); |
michael@0 | 39 | |
michael@0 | 40 | static SurfaceStream* CreateForType(SurfaceStreamType type, |
michael@0 | 41 | mozilla::gl::GLContext* glContext, |
michael@0 | 42 | SurfaceStream* prevStream = nullptr); |
michael@0 | 43 | |
michael@0 | 44 | SurfaceStreamHandle GetShareHandle() { |
michael@0 | 45 | return reinterpret_cast<SurfaceStreamHandle>(this); |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | static SurfaceStream* FromHandle(SurfaceStreamHandle handle) { |
michael@0 | 49 | return (SurfaceStream*)handle; |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | const SurfaceStreamType mType; |
michael@0 | 53 | |
michael@0 | 54 | mozilla::gl::GLContext* GLContext() const { return mGLContext; } |
michael@0 | 55 | |
michael@0 | 56 | |
michael@0 | 57 | protected: |
michael@0 | 58 | // |mProd| is owned by us, but can be ripped away when |
michael@0 | 59 | // creating a new GLStream from this one. |
michael@0 | 60 | SharedSurface* mProducer; |
michael@0 | 61 | std::set<SharedSurface*> mSurfaces; |
michael@0 | 62 | std::stack<SharedSurface*> mScraps; |
michael@0 | 63 | mutable Monitor mMonitor; |
michael@0 | 64 | bool mIsAlive; |
michael@0 | 65 | |
michael@0 | 66 | // Do not use this. It exists solely so we can ref it in CanvasClientWebGL::Update() |
michael@0 | 67 | // before sent up to the compositor. You have been warned (Bug 894405) |
michael@0 | 68 | mozilla::gl::GLContext* mGLContext; |
michael@0 | 69 | |
michael@0 | 70 | // |previous| can be null, indicating this is the first one. |
michael@0 | 71 | // Otherwise, we pull in |mProd| from |previous| an our initial surface. |
michael@0 | 72 | SurfaceStream(SurfaceStreamType type, SurfaceStream* prevStream) |
michael@0 | 73 | : mType(type) |
michael@0 | 74 | , mProducer(nullptr) |
michael@0 | 75 | , mMonitor("SurfaceStream monitor") |
michael@0 | 76 | , mIsAlive(true) |
michael@0 | 77 | { |
michael@0 | 78 | MOZ_ASSERT(!prevStream || mType != prevStream->mType, |
michael@0 | 79 | "We should not need to create a SurfaceStream from another " |
michael@0 | 80 | "of the same type."); |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | public: |
michael@0 | 84 | virtual ~SurfaceStream(); |
michael@0 | 85 | |
michael@0 | 86 | protected: |
michael@0 | 87 | // These functions below are helpers to make trading buffers around easier. |
michael@0 | 88 | // For instance, using Move(a,b) instead of normal assignment assures that |
michael@0 | 89 | // we are never leaving anything hanging around, keeping things very safe. |
michael@0 | 90 | static void Move(SharedSurface*& from, SharedSurface*& to) { |
michael@0 | 91 | MOZ_ASSERT(!to); |
michael@0 | 92 | to = from; |
michael@0 | 93 | from = nullptr; |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | void New(SurfaceFactory* factory, const gfx::IntSize& size, |
michael@0 | 97 | SharedSurface*& surf); |
michael@0 | 98 | void Delete(SharedSurface*& surf); |
michael@0 | 99 | void Recycle(SurfaceFactory* factory, SharedSurface*& surf); |
michael@0 | 100 | |
michael@0 | 101 | // Surrender control of a surface, and return it for use elsewhere. |
michael@0 | 102 | SharedSurface* Surrender(SharedSurface*& surf); |
michael@0 | 103 | // Absorb control of a surface from elsewhere, clears its old location. |
michael@0 | 104 | SharedSurface* Absorb(SharedSurface*& surf); |
michael@0 | 105 | |
michael@0 | 106 | // For holding on to surfaces we don't need until we can return them to the |
michael@0 | 107 | // Producer's factory via SurfaceFactory::Recycle. |
michael@0 | 108 | // Not thread-safe. |
michael@0 | 109 | void Scrap(SharedSurface*& scrap); |
michael@0 | 110 | |
michael@0 | 111 | // Not thread-safe. |
michael@0 | 112 | void RecycleScraps(SurfaceFactory* factory); |
michael@0 | 113 | |
michael@0 | 114 | public: |
michael@0 | 115 | /* Note that ownership of the returned surfaces below |
michael@0 | 116 | * transfers to the caller. |
michael@0 | 117 | * SwapProd returns null on failure. Returning null doesn't mean nothing |
michael@0 | 118 | * happened, but rather that a surface allocation failed. After returning |
michael@0 | 119 | * null, we must be able to call SwapProducer again with better args |
michael@0 | 120 | * and have everything work again. |
michael@0 | 121 | * One common failure is asking for a too-large |size|. |
michael@0 | 122 | */ |
michael@0 | 123 | virtual SharedSurface* SwapProducer(SurfaceFactory* factory, |
michael@0 | 124 | const gfx::IntSize& size) = 0; |
michael@0 | 125 | |
michael@0 | 126 | virtual SharedSurface* Resize(SurfaceFactory* factory, const gfx::IntSize& size); |
michael@0 | 127 | |
michael@0 | 128 | virtual bool CopySurfaceToProducer(SharedSurface* src, SurfaceFactory* factory) { MOZ_ASSERT(0); return false; } |
michael@0 | 129 | |
michael@0 | 130 | protected: |
michael@0 | 131 | // SwapCons will return the same surface more than once, |
michael@0 | 132 | // if nothing new has been published. |
michael@0 | 133 | virtual SharedSurface* SwapConsumer_NoWait() = 0; |
michael@0 | 134 | |
michael@0 | 135 | public: |
michael@0 | 136 | virtual SharedSurface* SwapConsumer(); |
michael@0 | 137 | |
michael@0 | 138 | virtual void SurrenderSurfaces(SharedSurface*& producer, SharedSurface*& consumer) = 0; |
michael@0 | 139 | }; |
michael@0 | 140 | |
michael@0 | 141 | // Not thread-safe. Don't use cross-threads. |
michael@0 | 142 | class SurfaceStream_SingleBuffer |
michael@0 | 143 | : public SurfaceStream |
michael@0 | 144 | { |
michael@0 | 145 | protected: |
michael@0 | 146 | SharedSurface* mConsumer; // Only present after resize-swap. |
michael@0 | 147 | |
michael@0 | 148 | public: |
michael@0 | 149 | MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(SurfaceStream_SingleBuffer) |
michael@0 | 150 | SurfaceStream_SingleBuffer(SurfaceStream* prevStream); |
michael@0 | 151 | virtual ~SurfaceStream_SingleBuffer(); |
michael@0 | 152 | |
michael@0 | 153 | /* Since we're non-OMTC, we know the order of execution here: |
michael@0 | 154 | * SwapProd gets called in UpdateSurface, followed by |
michael@0 | 155 | * SwapCons being called in Render. |
michael@0 | 156 | */ |
michael@0 | 157 | virtual SharedSurface* SwapProducer(SurfaceFactory* factory, |
michael@0 | 158 | const gfx::IntSize& size); |
michael@0 | 159 | |
michael@0 | 160 | virtual SharedSurface* SwapConsumer_NoWait(); |
michael@0 | 161 | |
michael@0 | 162 | virtual void SurrenderSurfaces(SharedSurface*& producer, SharedSurface*& consumer); |
michael@0 | 163 | }; |
michael@0 | 164 | |
michael@0 | 165 | // Our hero for preserveDrawingBuffer=true. |
michael@0 | 166 | class SurfaceStream_TripleBuffer_Copy |
michael@0 | 167 | : public SurfaceStream |
michael@0 | 168 | { |
michael@0 | 169 | protected: |
michael@0 | 170 | SharedSurface* mStaging; |
michael@0 | 171 | SharedSurface* mConsumer; |
michael@0 | 172 | |
michael@0 | 173 | public: |
michael@0 | 174 | MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(SurfaceStream_TripleBuffer_Copy) |
michael@0 | 175 | SurfaceStream_TripleBuffer_Copy(SurfaceStream* prevStream); |
michael@0 | 176 | virtual ~SurfaceStream_TripleBuffer_Copy(); |
michael@0 | 177 | |
michael@0 | 178 | virtual SharedSurface* SwapProducer(SurfaceFactory* factory, |
michael@0 | 179 | const gfx::IntSize& size); |
michael@0 | 180 | |
michael@0 | 181 | virtual SharedSurface* SwapConsumer_NoWait(); |
michael@0 | 182 | |
michael@0 | 183 | virtual void SurrenderSurfaces(SharedSurface*& producer, SharedSurface*& consumer); |
michael@0 | 184 | }; |
michael@0 | 185 | |
michael@0 | 186 | |
michael@0 | 187 | class SurfaceStream_TripleBuffer |
michael@0 | 188 | : public SurfaceStream |
michael@0 | 189 | { |
michael@0 | 190 | protected: |
michael@0 | 191 | SharedSurface* mStaging; |
michael@0 | 192 | SharedSurface* mConsumer; |
michael@0 | 193 | |
michael@0 | 194 | // Returns true if we were able to wait, false if not |
michael@0 | 195 | virtual bool WaitForCompositor() { return false; } |
michael@0 | 196 | |
michael@0 | 197 | // To support subclasses initializing the mType. |
michael@0 | 198 | SurfaceStream_TripleBuffer(SurfaceStreamType type, SurfaceStream* prevStream); |
michael@0 | 199 | |
michael@0 | 200 | public: |
michael@0 | 201 | MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(SurfaceStream_TripleBuffer) |
michael@0 | 202 | SurfaceStream_TripleBuffer(SurfaceStream* prevStream); |
michael@0 | 203 | virtual ~SurfaceStream_TripleBuffer(); |
michael@0 | 204 | virtual bool CopySurfaceToProducer(SharedSurface* src, SurfaceFactory* factory); |
michael@0 | 205 | |
michael@0 | 206 | private: |
michael@0 | 207 | // Common constructor code. |
michael@0 | 208 | void Init(SurfaceStream* prevStream); |
michael@0 | 209 | |
michael@0 | 210 | public: |
michael@0 | 211 | // Done writing to prod, swap prod and staging |
michael@0 | 212 | virtual SharedSurface* SwapProducer(SurfaceFactory* factory, |
michael@0 | 213 | const gfx::IntSize& size); |
michael@0 | 214 | |
michael@0 | 215 | virtual SharedSurface* SwapConsumer_NoWait(); |
michael@0 | 216 | |
michael@0 | 217 | virtual void SurrenderSurfaces(SharedSurface*& producer, SharedSurface*& consumer); |
michael@0 | 218 | }; |
michael@0 | 219 | |
michael@0 | 220 | class SurfaceStream_TripleBuffer_Async |
michael@0 | 221 | : public SurfaceStream_TripleBuffer |
michael@0 | 222 | { |
michael@0 | 223 | protected: |
michael@0 | 224 | virtual bool WaitForCompositor() MOZ_OVERRIDE; |
michael@0 | 225 | |
michael@0 | 226 | public: |
michael@0 | 227 | SurfaceStream_TripleBuffer_Async(SurfaceStream* prevStream); |
michael@0 | 228 | virtual ~SurfaceStream_TripleBuffer_Async(); |
michael@0 | 229 | }; |
michael@0 | 230 | |
michael@0 | 231 | |
michael@0 | 232 | } /* namespace gfx */ |
michael@0 | 233 | } /* namespace mozilla */ |
michael@0 | 234 | |
michael@0 | 235 | #endif /* SURFACESTREAM_H_ */ |