1 /* -*- Mode: C++; tab-width: 4; 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/. */
5 
6 #ifndef TEX_UNPACK_BLOB_H_
7 #define TEX_UNPACK_BLOB_H_
8 
9 #include "GLContextTypes.h"
10 #include "mozilla/RefPtr.h"
11 #include "WebGLStrongTypes.h"
12 #include "WebGLTypes.h"
13 
14 namespace mozilla {
15 
16 class UniqueBuffer;
17 class WebGLContext;
18 class WebGLTexture;
19 
20 namespace dom {
21 class Element;
22 class HTMLCanvasElement;
23 class HTMLVideoElement;
24 }  // namespace dom
25 
26 namespace gfx {
27 class DataSourceSurface;
28 }  // namespace gfx
29 
30 namespace layers {
31 class Image;
32 class ImageContainer;
33 }  // namespace layers
34 
35 namespace webgl {
36 
37 struct PackingInfo;
38 struct DriverUnpackInfo;
39 
40 class TexUnpackBlob {
41  public:
42   const uint32_t mAlignment;
43   const uint32_t mRowLength;
44   const uint32_t mImageHeight;
45   const uint32_t mSkipPixels;
46   const uint32_t mSkipRows;
47   const uint32_t mSkipImages;
48   const uint32_t mWidth;
49   const uint32_t mHeight;
50   const uint32_t mDepth;
51 
52   const gfxAlphaType mSrcAlphaType;
53 
54   bool mNeedsExactUpload;
55 
56  protected:
57   TexUnpackBlob(const WebGLContext* webgl, TexImageTarget target,
58                 uint32_t rowLength, uint32_t width, uint32_t height,
59                 uint32_t depth, gfxAlphaType srcAlphaType);
60 
61  public:
62   virtual ~TexUnpackBlob() = default;
63 
64  protected:
65   bool ConvertIfNeeded(WebGLContext* webgl, const uint32_t rowLength,
66                        const uint32_t rowCount, WebGLTexelFormat srcFormat,
67                        const uint8_t* const srcBegin, const ptrdiff_t srcStride,
68                        WebGLTexelFormat dstFormat, const ptrdiff_t dstStride,
69 
70                        const uint8_t** const out_begin,
71                        UniqueBuffer* const out_anchoredBuffer) const;
72 
73  public:
HasData()74   virtual bool HasData() const { return true; }
75 
76   virtual bool Validate(WebGLContext* webgl, const webgl::PackingInfo& pi) = 0;
77 
78   // Returns false when we've generated a WebGL error.
79   // Returns true but with a non-zero *out_error if we still need to generate a
80   // WebGL error.
81   virtual bool TexOrSubImage(bool isSubImage, bool needsRespec,
82                              WebGLTexture* tex, TexImageTarget target,
83                              GLint level, const webgl::DriverUnpackInfo* dui,
84                              GLint xOffset, GLint yOffset, GLint zOffset,
85                              const webgl::PackingInfo& pi,
86                              GLenum* const out_error) const = 0;
87 };
88 
89 class TexUnpackBytes final : public TexUnpackBlob {
90  public:
91   const bool mIsClientData;
92   const uint8_t* const mPtr;
93   const size_t mAvailBytes;
94 
95   TexUnpackBytes(const WebGLContext* webgl, TexImageTarget target,
96                  uint32_t width, uint32_t height, uint32_t depth,
97                  bool isClientData, const uint8_t* ptr, size_t availBytes);
98 
HasData()99   virtual bool HasData() const override { return !mIsClientData || bool(mPtr); }
100 
101   virtual bool Validate(WebGLContext* webgl,
102                         const webgl::PackingInfo& pi) override;
103   virtual bool TexOrSubImage(bool isSubImage, bool needsRespec,
104                              WebGLTexture* tex, TexImageTarget target,
105                              GLint level, const webgl::DriverUnpackInfo* dui,
106                              GLint xOffset, GLint yOffset, GLint zOffset,
107                              const webgl::PackingInfo& pi,
108                              GLenum* const out_error) const override;
109 };
110 
111 class TexUnpackImage final : public TexUnpackBlob {
112  public:
113   const RefPtr<layers::Image> mImage;
114 
115   TexUnpackImage(const WebGLContext* webgl, TexImageTarget target,
116                  uint32_t width, uint32_t height, uint32_t depth,
117                  layers::Image* image, gfxAlphaType srcAlphaType);
118 
119   ~TexUnpackImage();  // Prevent needing to define layers::Image in the header.
120 
121   virtual bool Validate(WebGLContext* webgl,
122                         const webgl::PackingInfo& pi) override;
123   virtual bool TexOrSubImage(bool isSubImage, bool needsRespec,
124                              WebGLTexture* tex, TexImageTarget target,
125                              GLint level, const webgl::DriverUnpackInfo* dui,
126                              GLint xOffset, GLint yOffset, GLint zOffset,
127                              const webgl::PackingInfo& dstPI,
128                              GLenum* const out_error) const override;
129 };
130 
131 class TexUnpackSurface final : public TexUnpackBlob {
132  public:
133   const RefPtr<gfx::DataSourceSurface> mSurf;
134 
135   TexUnpackSurface(const WebGLContext* webgl, TexImageTarget target,
136                    uint32_t width, uint32_t height, uint32_t depth,
137                    gfx::DataSourceSurface* surf, gfxAlphaType srcAlphaType);
138 
139   virtual bool Validate(WebGLContext* webgl,
140                         const webgl::PackingInfo& pi) override;
141   virtual bool TexOrSubImage(bool isSubImage, bool needsRespec,
142                              WebGLTexture* tex, TexImageTarget target,
143                              GLint level, const webgl::DriverUnpackInfo* dui,
144                              GLint xOffset, GLint yOffset, GLint zOffset,
145                              const webgl::PackingInfo& dstPI,
146                              GLenum* const out_error) const override;
147 };
148 
149 }  // namespace webgl
150 }  // namespace mozilla
151 
152 #endif  // TEX_UNPACK_BLOB_H_
153