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