GLImages.h (3863B)
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 GFX_GLIMAGES_H 8 #define GFX_GLIMAGES_H 9 10 #include "GLContextTypes.h" 11 #include "GLTypes.h" 12 #include "ImageContainer.h" // for Image 13 #include "ImageTypes.h" // for ImageFormat::SHARED_GLTEXTURE 14 #include "nsCOMPtr.h" // for already_AddRefed 15 #include "mozilla/Maybe.h" // for Maybe 16 #include "mozilla/gfx/Matrix.h" // for Matrix4x4 17 #include "mozilla/gfx/Point.h" // for IntSize 18 19 #ifdef MOZ_WIDGET_ANDROID 20 # include "AndroidSurfaceTexture.h" 21 #endif 22 23 namespace mozilla { 24 namespace layers { 25 26 class GLImage : public Image { 27 public: 28 explicit GLImage(ImageFormat aFormat) : Image(nullptr, aFormat) {} 29 30 already_AddRefed<gfx::SourceSurface> GetAsSourceSurface() override; 31 32 nsresult BuildSurfaceDescriptorBuffer( 33 SurfaceDescriptorBuffer& aSdBuffer, BuildSdbFlags aFlags, 34 const std::function<MemoryOrShmem(uint32_t)>& aAllocate) override; 35 36 GLImage* AsGLImage() override { return this; } 37 38 protected: 39 nsresult ReadIntoBuffer(uint8_t* aData, int32_t aStride, 40 const gfx::IntSize& aSize, 41 gfx::SurfaceFormat aFormat); 42 }; 43 44 #ifdef MOZ_WIDGET_ANDROID 45 46 class SurfaceTextureImage final : public GLImage { 47 public: 48 class SetCurrentCallback { 49 public: 50 virtual void operator()(void) = 0; 51 virtual ~SetCurrentCallback() {} 52 }; 53 54 SurfaceTextureImage(AndroidSurfaceTextureHandle aHandle, 55 const gfx::IntSize& aSize, bool aContinuous, 56 gl::OriginPos aOriginPos, bool aHasAlpha, 57 bool aForceBT709ColorSpace, 58 Maybe<gfx::Matrix4x4> aTransformOverride); 59 60 gfx::IntSize GetSize() const override { return mSize; } 61 AndroidSurfaceTextureHandle GetHandle() const { return mHandle; } 62 bool GetContinuous() const { return mContinuous; } 63 gl::OriginPos GetOriginPos() const { return mOriginPos; } 64 bool GetHasAlpha() const { return mHasAlpha; } 65 bool GetForceBT709ColorSpace() const { return mForceBT709ColorSpace; } 66 const Maybe<gfx::Matrix4x4>& GetTransformOverride() const { 67 return mTransformOverride; 68 } 69 70 already_AddRefed<gfx::SourceSurface> GetAsSourceSurface() override { 71 // We can implement this, but currently don't want to because it will cause 72 // the SurfaceTexture to be permanently bound to the snapshot readback 73 // context. 74 return nullptr; 75 } 76 77 nsresult BuildSurfaceDescriptorBuffer( 78 SurfaceDescriptorBuffer& aSdBuffer, BuildSdbFlags aFlags, 79 const std::function<MemoryOrShmem(uint32_t)>& aAllocate) override { 80 // We can implement this, but currently don't want to because it will cause 81 // the SurfaceTexture to be permanently bound to the snapshot readback 82 // context. 83 return NS_ERROR_NOT_IMPLEMENTED; 84 } 85 86 SurfaceTextureImage* AsSurfaceTextureImage() override { return this; } 87 88 Maybe<SurfaceDescriptor> GetDesc() override; 89 90 void RegisterSetCurrentCallback(UniquePtr<SetCurrentCallback> aCallback) { 91 mSetCurrentCallback = std::move(aCallback); 92 } 93 94 void OnSetCurrent() override { 95 if (mSetCurrentCallback) { 96 (*mSetCurrentCallback)(); 97 mSetCurrentCallback.reset(); 98 } 99 } 100 101 private: 102 AndroidSurfaceTextureHandle mHandle; 103 gfx::IntSize mSize; 104 bool mContinuous; 105 gl::OriginPos mOriginPos; 106 const bool mHasAlpha; 107 const bool mForceBT709ColorSpace; 108 const Maybe<gfx::Matrix4x4> mTransformOverride; 109 UniquePtr<SetCurrentCallback> mSetCurrentCallback; 110 }; 111 112 #endif // MOZ_WIDGET_ANDROID 113 114 } // namespace layers 115 } // namespace mozilla 116 117 #endif // GFX_GLIMAGES_H