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 MOZILLA_GFX_TEXTURECLIENTOGL_H
8 #define MOZILLA_GFX_TEXTURECLIENTOGL_H
9 
10 #include "GLContextTypes.h"  // for SharedTextureHandle, etc
11 #include "GLImages.h"
12 #include "gfxTypes.h"
13 #include "mozilla/Attributes.h"  // for override
14 #include "mozilla/gfx/Point.h"   // for IntSize
15 #include "mozilla/gfx/Types.h"   // for SurfaceFormat
16 #include "mozilla/layers/CompositorTypes.h"
17 #include "mozilla/layers/LayersSurfaces.h"  // for SurfaceDescriptor
18 #include "mozilla/layers/TextureClient.h"   // for TextureClient, etc
19 #ifdef MOZ_WIDGET_ANDROID
20 #  include "AndroidSurfaceTexture.h"
21 #  include "AndroidNativeWindow.h"
22 #  include "mozilla/java/GeckoSurfaceWrappers.h"
23 #  include "mozilla/layers/AndroidHardwareBuffer.h"
24 #endif
25 
26 namespace mozilla {
27 
28 namespace gfx {
29 
30 class DrawTarget;
31 
32 }  // namespace gfx
33 
34 namespace layers {
35 
36 #ifdef MOZ_WIDGET_ANDROID
37 
38 class AndroidHardwareBuffer;
39 
40 class AndroidSurfaceTextureData : public TextureData {
41  public:
42   static already_AddRefed<TextureClient> CreateTextureClient(
43       AndroidSurfaceTextureHandle aHandle, gfx::IntSize aSize, bool aContinuous,
44       gl::OriginPos aOriginPos, bool aHasAlpha, LayersIPCChannel* aAllocator,
45       TextureFlags aFlags);
46 
47   virtual ~AndroidSurfaceTextureData();
48 
49   void FillInfo(TextureData::Info& aInfo) const override;
50 
51   bool Serialize(SurfaceDescriptor& aOutDescriptor) override;
52 
53   // Useless functions.
Lock(OpenMode)54   bool Lock(OpenMode) override { return true; }
55 
Unlock()56   void Unlock() override {}
57 
58   // Our data is always owned externally.
Deallocate(LayersIPCChannel *)59   void Deallocate(LayersIPCChannel*) override {}
60 
61  protected:
62   AndroidSurfaceTextureData(AndroidSurfaceTextureHandle aHandle,
63                             gfx::IntSize aSize, bool aContinuous,
64                             bool aHasAlpha);
65 
66   const AndroidSurfaceTextureHandle mHandle;
67   const gfx::IntSize mSize;
68   const bool mContinuous;
69   const bool mHasAlpha;
70 };
71 
72 class AndroidNativeWindowTextureData : public TextureData {
73  public:
74   static AndroidNativeWindowTextureData* Create(gfx::IntSize aSize,
75                                                 gfx::SurfaceFormat aFormat);
76 
77   void FillInfo(TextureData::Info& aInfo) const override;
78 
79   bool Serialize(SurfaceDescriptor& aOutDescriptor) override;
80 
81   bool Lock(OpenMode) override;
82   void Unlock() override;
83 
84   void Forget(LayersIPCChannel*) override;
Deallocate(LayersIPCChannel *)85   void Deallocate(LayersIPCChannel*) override {}
86 
87   already_AddRefed<gfx::DrawTarget> BorrowDrawTarget() override;
88 
89   void OnForwardedToHost() override;
90 
91  protected:
92   AndroidNativeWindowTextureData(java::GeckoSurface::Param aSurface,
93                                  gfx::IntSize aSize,
94                                  gfx::SurfaceFormat aFormat);
95 
96  private:
97   java::GeckoSurface::GlobalRef mSurface;
98   ANativeWindow* mNativeWindow;
99   ANativeWindow_Buffer mBuffer;
100   // Keeps track of whether the underlying NativeWindow is actually locked.
101   bool mIsLocked;
102 
103   const gfx::IntSize mSize;
104   const gfx::SurfaceFormat mFormat;
105 };
106 
107 class AndroidHardwareBufferTextureData : public TextureData {
108  public:
109   static AndroidHardwareBufferTextureData* Create(gfx::IntSize aSize,
110                                                   gfx::SurfaceFormat aFormat);
111 
112   virtual ~AndroidHardwareBufferTextureData();
113 
114   void FillInfo(TextureData::Info& aInfo) const override;
115 
116   bool Serialize(SurfaceDescriptor& aOutDescriptor) override;
117 
118   bool Lock(OpenMode aMode) override;
119   void Unlock() override;
120 
121   void Forget(LayersIPCChannel*) override;
Deallocate(LayersIPCChannel *)122   void Deallocate(LayersIPCChannel*) override {}
123 
124   already_AddRefed<gfx::DrawTarget> BorrowDrawTarget() override;
125 
126   void OnForwardedToHost() override;
127 
128   TextureFlags GetTextureFlags() const override;
129 
130   Maybe<uint64_t> GetBufferId() const override;
131 
132   mozilla::ipc::FileDescriptor GetAcquireFence() override;
133 
AsAndroidHardwareBufferTextureData()134   AndroidHardwareBufferTextureData* AsAndroidHardwareBufferTextureData()
135       override {
136     return this;
137   }
138 
GetBuffer()139   AndroidHardwareBuffer* GetBuffer() { return mAndroidHardwareBuffer; }
140 
141  protected:
142   AndroidHardwareBufferTextureData(
143       AndroidHardwareBuffer* aAndroidHardwareBuffer, gfx::IntSize aSize,
144       gfx::SurfaceFormat aFormat);
145 
146   RefPtr<AndroidHardwareBuffer> mAndroidHardwareBuffer;
147   const gfx::IntSize mSize;
148   const gfx::SurfaceFormat mFormat;
149 
150   void* mAddress;
151 
152   // Keeps track of whether the underlying NativeWindow is actually locked.
153   bool mIsLocked;
154 };
155 
156 #endif  // MOZ_WIDGET_ANDROID
157 
158 }  // namespace layers
159 }  // namespace mozilla
160 
161 #endif
162