summaryrefslogtreecommitdiffstats
path: root/dom/canvas/WebGL2ContextFramebuffers.cpp
blob: ab9e848f620ec635b852d191a18bc56af4f71c54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "WebGL2Context.h"

#include "GLContext.h"
#include "GLScreenBuffer.h"
#include "mozilla/CheckedInt.h"
#include "WebGLContextUtils.h"
#include "WebGLFormats.h"
#include "WebGLFramebuffer.h"

namespace mozilla {

void WebGL2Context::BlitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1,
                                    GLint srcY1, GLint dstX0, GLint dstY0,
                                    GLint dstX1, GLint dstY1, GLbitfield mask,
                                    GLenum filter) {
  const FuncScope funcScope(*this, "blitFramebuffer");
  if (IsContextLost()) return;

  const GLbitfield validBits = LOCAL_GL_COLOR_BUFFER_BIT |
                               LOCAL_GL_DEPTH_BUFFER_BIT |
                               LOCAL_GL_STENCIL_BUFFER_BIT;
  if ((mask | validBits) != validBits) {
    ErrorInvalidValue("Invalid bit set in mask.");
    return;
  }

  switch (filter) {
    case LOCAL_GL_NEAREST:
    case LOCAL_GL_LINEAR:
      break;
    default:
      ErrorInvalidEnumInfo("filter", filter);
      return;
  }

  // --

  const auto fnLikelyOverflow = [](GLint p0, GLint p1) {
    auto checked = CheckedInt<GLint>(p1) - p0;
    checked = -checked;  // And check the negation!
    return !checked.isValid();
  };

  if (fnLikelyOverflow(srcX0, srcX1) || fnLikelyOverflow(srcY0, srcY1) ||
      fnLikelyOverflow(dstX0, dstX1) || fnLikelyOverflow(dstY0, dstY1)) {
    ErrorInvalidValue("Likely-to-overflow large ranges are forbidden.");
    return;
  }

  // --

  if (!ValidateAndInitFB(mBoundReadFramebuffer) ||
      !ValidateAndInitFB(mBoundDrawFramebuffer)) {
    return;
  }

  DoBindFB(mBoundReadFramebuffer, LOCAL_GL_READ_FRAMEBUFFER);
  DoBindFB(mBoundDrawFramebuffer, LOCAL_GL_DRAW_FRAMEBUFFER);

  WebGLFramebuffer::BlitFramebuffer(this, srcX0, srcY0, srcX1, srcY1, dstX0,
                                    dstY0, dstX1, dstY1, mask, filter);
}

////

static bool ValidateBackbufferAttachmentEnum(WebGLContext* webgl,
                                             GLenum attachment) {
  switch (attachment) {
    case LOCAL_GL_COLOR:
    case LOCAL_GL_DEPTH:
    case LOCAL_GL_STENCIL:
      return true;

    default:
      webgl->ErrorInvalidEnumInfo("attachment", attachment);
      return false;
  }
}

static bool ValidateFramebufferAttachmentEnum(WebGLContext* webgl,
                                              GLenum attachment) {
  switch (attachment) {
    case LOCAL_GL_DEPTH_ATTACHMENT:
    case LOCAL_GL_STENCIL_ATTACHMENT:
    case LOCAL_GL_DEPTH_STENCIL_ATTACHMENT:
      return true;
  }

  if (attachment < LOCAL_GL_COLOR_ATTACHMENT0) {
    webgl->ErrorInvalidEnumInfo("attachment", attachment);
    return false;
  }

  if (attachment > webgl->LastColorAttachmentEnum()) {
    // That these errors have different types is ridiculous.
    webgl->ErrorInvalidOperation("Too-large LOCAL_GL_COLOR_ATTACHMENTn.");
    return false;
  }

  return true;
}

bool WebGLContext::ValidateInvalidateFramebuffer(
    GLenum target, const Span<const GLenum>& attachments,
    std::vector<GLenum>* const scopedVector,
    GLsizei* const out_glNumAttachments,
    const GLenum** const out_glAttachments) {
  if (IsContextLost()) return false;

  if (!ValidateFramebufferTarget(target)) return false;

  const WebGLFramebuffer* fb;
  bool isDefaultFB = false;
  switch (target) {
    case LOCAL_GL_FRAMEBUFFER:
    case LOCAL_GL_DRAW_FRAMEBUFFER:
      fb = mBoundDrawFramebuffer;
      break;

    case LOCAL_GL_READ_FRAMEBUFFER:
      fb = mBoundReadFramebuffer;
      break;

    default:
      MOZ_CRASH("GFX: Bad target.");
  }

  if (fb) {
    const auto fbStatus = fb->CheckFramebufferStatus();
    if (fbStatus != LOCAL_GL_FRAMEBUFFER_COMPLETE)
      return false;  // Not an error, but don't run forward to driver either.
  } else {
    if (!EnsureDefaultFB()) return false;
  }
  DoBindFB(fb, target);

  *out_glNumAttachments = AutoAssertCast(attachments.size());
  *out_glAttachments = attachments.data();

  if (fb) {
    for (const auto& attachment : attachments) {
      if (!ValidateFramebufferAttachmentEnum(this, attachment)) return false;
    }
  } else {
    for (const auto& attachment : attachments) {
      if (!ValidateBackbufferAttachmentEnum(this, attachment)) return false;
    }

    if (!isDefaultFB) {
      MOZ_ASSERT(scopedVector->empty());
      scopedVector->reserve(attachments.size());
      for (const auto& attachment : attachments) {
        switch (attachment) {
          case LOCAL_GL_COLOR:
            scopedVector->push_back(LOCAL_GL_COLOR_ATTACHMENT0);
            break;

          case LOCAL_GL_DEPTH:
            scopedVector->push_back(LOCAL_GL_DEPTH_ATTACHMENT);
            break;

          case LOCAL_GL_STENCIL:
            scopedVector->push_back(LOCAL_GL_STENCIL_ATTACHMENT);
            break;

          default:
            MOZ_CRASH();
        }
      }
      *out_glNumAttachments = AutoAssertCast(scopedVector->size());
      *out_glAttachments = scopedVector->data();
    }
  }

  ////

  return true;
}

void WebGL2Context::InvalidateFramebuffer(
    GLenum target, const Span<const GLenum>& attachments) {
  const FuncScope funcScope(*this, "invalidateFramebuffer");

  std::vector<GLenum> scopedVector;
  GLsizei glNumAttachments;
  const GLenum* glAttachments;
  if (!ValidateInvalidateFramebuffer(target, attachments, &scopedVector,
                                     &glNumAttachments, &glAttachments)) {
    return;
  }

  ////

  // Some drivers (like OSX 10.9 GL) just don't support invalidate_framebuffer.
  const bool useFBInvalidation =
      (mAllowFBInvalidation &&
       gl->IsSupported(gl::GLFeature::invalidate_framebuffer));
  if (useFBInvalidation) {
    gl->fInvalidateFramebuffer(target, glNumAttachments, glAttachments);
    return;
  }

  // Use clear instead?
  // No-op for now.
}

void WebGL2Context::InvalidateSubFramebuffer(
    GLenum target, const Span<const GLenum>& attachments, GLint x, GLint y,
    GLsizei width, GLsizei height) {
  const FuncScope funcScope(*this, "invalidateSubFramebuffer");

  std::vector<GLenum> scopedVector;
  GLsizei glNumAttachments;
  const GLenum* glAttachments;
  if (!ValidateInvalidateFramebuffer(target, attachments, &scopedVector,
                                     &glNumAttachments, &glAttachments)) {
    return;
  }

  if (!ValidateNonNegative("width", width) ||
      !ValidateNonNegative("height", height)) {
    return;
  }

  ////

  // Some drivers (like OSX 10.9 GL) just don't support invalidate_framebuffer.
  const bool useFBInvalidation =
      (mAllowFBInvalidation &&
       gl->IsSupported(gl::GLFeature::invalidate_framebuffer));
  if (useFBInvalidation) {
    gl->fInvalidateSubFramebuffer(target, glNumAttachments, glAttachments, x, y,
                                  width, height);
    return;
  }

  // Use clear instead?
  // No-op for now.
}

void WebGL2Context::ReadBuffer(GLenum mode) {
  const FuncScope funcScope(*this, "readBuffer");
  if (IsContextLost()) return;

  if (mBoundReadFramebuffer) {
    mBoundReadFramebuffer->ReadBuffer(mode);
    return;
  }

  // Operating on the default framebuffer.
  if (mode != LOCAL_GL_NONE && mode != LOCAL_GL_BACK) {
    nsCString enumName;
    EnumName(mode, &enumName);
    ErrorInvalidOperation(
        "If READ_FRAMEBUFFER is null, `mode` must be BACK or"
        " NONE. Was %s.",
        enumName.BeginReading());
    return;
  }

  mDefaultFB_ReadBuffer = mode;
}

}  // namespace mozilla