/
BitmapTexture.cpp
320 lines (265 loc) · 11 KB
/
BitmapTexture.cpp
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
Copyright (C) 2012 Igalia S.L.
Copyright (C) 2012 Adobe Systems Incorporated
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "BitmapTexture.h"
#if USE(TEXTURE_MAPPER)
#include "GLContext.h"
#include "GraphicsContext.h"
#include "GraphicsLayer.h"
#include "ImageBuffer.h"
#include "LengthFunctions.h"
#include "NativeImage.h"
#include "TextureMapper.h"
#include "TextureMapperFlags.h"
#include "TextureMapperShaderProgram.h"
#include <wtf/HashMap.h>
#include <wtf/RefCounted.h>
#include <wtf/RefPtr.h>
#if USE(CAIRO)
#include "CairoUtilities.h"
#include "RefPtrCairo.h"
#include <cairo.h>
#include <wtf/text/CString.h>
#endif
#if OS(DARWIN)
#define GL_UNSIGNED_INT_8_8_8_8_REV 0x8367
static const GLenum s_pixelDataType = GL_UNSIGNED_INT_8_8_8_8_REV;
#else
static const GLenum s_pixelDataType = GL_UNSIGNED_BYTE;
#endif
namespace WebCore {
BitmapTexture::BitmapTexture(const IntSize& size, OptionSet<Flags> flags, GLint internalFormat)
: m_flags(flags)
, m_size(size)
, m_internalFormat(internalFormat == GL_DONT_CARE ? GL_RGBA : internalFormat)
, m_format(GL_RGBA)
{
glGenTextures(1, &m_id);
glBindTexture(GL_TEXTURE_2D, m_id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, m_internalFormat, m_size.width(), m_size.height(), 0, m_format, s_pixelDataType, nullptr);
}
void BitmapTexture::reset(const IntSize& size, OptionSet<Flags> flags)
{
m_flags = flags;
m_shouldClear = true;
m_colorConvertFlags = { };
m_filterOperation = nullptr;
if (m_size != size) {
m_size = size;
glBindTexture(GL_TEXTURE_2D, m_id);
glTexImage2D(GL_TEXTURE_2D, 0, m_internalFormat, m_size.width(), m_size.height(), 0, m_format, s_pixelDataType, nullptr);
}
}
void BitmapTexture::updateContents(const void* srcData, const IntRect& targetRect, const IntPoint& sourceOffset, int bytesPerLine)
{
// We are updating a texture with format RGBA with content from a buffer that has BGRA format. Instead of turning BGRA
// into RGBA and then uploading it, we upload it as is. This causes the texture format to be RGBA but the content to be BGRA,
// so we mark the texture to convert the colors when painting the texture.
#if CPU(LITTLE_ENDIAN)
m_colorConvertFlags = TextureMapperFlags::ShouldConvertTextureBGRAToRGBA;
#else
m_colorConvertFlags = TextureMapperFlags::ShouldConvertTextureARGBToRGBA;
#endif
glBindTexture(GL_TEXTURE_2D, m_id);
const unsigned bytesPerPixel = 4;
auto data = static_cast<const uint8_t*>(srcData);
Vector<uint8_t> temporaryData;
IntPoint adjustedSourceOffset = sourceOffset;
// Texture upload requires subimage buffer if driver doesn't support subimage and we don't have full image upload.
bool supportsUnpackSubimage = GLContext::current()->glExtensions().EXT_unpack_subimage;
bool requireSubImageBuffer = !supportsUnpackSubimage
&& !(bytesPerLine == static_cast<int>(targetRect.width() * bytesPerPixel) && adjustedSourceOffset == IntPoint::zero());
// prepare temporaryData if necessary
if (requireSubImageBuffer) {
temporaryData.resize(targetRect.width() * targetRect.height() * bytesPerPixel);
auto dst = temporaryData.data();
data = dst;
auto bits = static_cast<const uint8_t*>(srcData);
auto src = bits + sourceOffset.y() * bytesPerLine + sourceOffset.x() * bytesPerPixel;
const int targetBytesPerLine = targetRect.width() * bytesPerPixel;
for (int y = 0; y < targetRect.height(); ++y) {
memcpy(dst, src, targetBytesPerLine);
src += bytesPerLine;
dst += targetBytesPerLine;
}
bytesPerLine = targetBytesPerLine;
adjustedSourceOffset = IntPoint(0, 0);
}
glBindTexture(GL_TEXTURE_2D, m_id);
if (supportsUnpackSubimage) {
// Use the OpenGL sub-image extension, now that we know it's available.
glPixelStorei(GL_UNPACK_ROW_LENGTH, bytesPerLine / bytesPerPixel);
glPixelStorei(GL_UNPACK_SKIP_ROWS, adjustedSourceOffset.y());
glPixelStorei(GL_UNPACK_SKIP_PIXELS, adjustedSourceOffset.x());
}
glTexSubImage2D(GL_TEXTURE_2D, 0, targetRect.x(), targetRect.y(), targetRect.width(), targetRect.height(), m_format, s_pixelDataType, data);
if (supportsUnpackSubimage) {
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
}
}
void BitmapTexture::updateContents(NativeImage* frameImage, const IntRect& targetRect, const IntPoint& offset)
{
if (!frameImage)
return;
#if USE(CAIRO)
cairo_surface_t* surface = frameImage->platformImage().get();
const uint8_t* imageData = cairo_image_surface_get_data(surface);
int bytesPerLine = cairo_image_surface_get_stride(surface);
updateContents(imageData, targetRect, offset, bytesPerLine);
#else
UNUSED_PARAM(targetRect);
UNUSED_PARAM(offset);
RELEASE_ASSERT_NOT_REACHED();
#endif
}
void BitmapTexture::updateContents(GraphicsLayer* sourceLayer, const IntRect& targetRect, const IntPoint& offset, float scale)
{
// Making an unconditionally unaccelerated buffer here is OK because this code
// isn't used by any platforms that respect the accelerated bit.
auto imageBuffer = ImageBuffer::create(targetRect.size(), RenderingPurpose::Unspecified, 1, DestinationColorSpace::SRGB(), PixelFormat::BGRA8);
if (!imageBuffer)
return;
GraphicsContext& context = imageBuffer->context();
context.setTextDrawingMode(TextDrawingMode::Fill);
IntRect sourceRect(targetRect);
sourceRect.setLocation(offset);
sourceRect.scale(1 / scale);
context.applyDeviceScaleFactor(scale);
context.translate(-sourceRect.x(), -sourceRect.y());
sourceLayer->paintGraphicsLayerContents(context, sourceRect);
auto image = ImageBuffer::sinkIntoNativeImage(WTFMove(imageBuffer));
if (!image)
return;
updateContents(image.get(), targetRect, IntPoint());
}
void BitmapTexture::initializeStencil()
{
#if !USE(TEXMAP_DEPTH_STENCIL_BUFFER)
if (m_rbo)
return;
glGenRenderbuffers(1, &m_rbo);
glBindRenderbuffer(GL_RENDERBUFFER, m_rbo);
glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, m_size.width(), m_size.height());
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, m_rbo);
glClearStencil(0);
glClear(GL_STENCIL_BUFFER_BIT);
#endif
}
void BitmapTexture::initializeDepthBuffer()
{
if (m_depthBufferObject)
return;
#if USE(TEXMAP_DEPTH_STENCIL_BUFFER)
GLenum format = GL_DEPTH24_STENCIL8_OES;
#else
GLenum format = GL_DEPTH_COMPONENT16;
#endif
glGenRenderbuffers(1, &m_depthBufferObject);
glBindRenderbuffer(GL_RENDERBUFFER, m_depthBufferObject);
glRenderbufferStorage(GL_RENDERBUFFER, format, m_size.width(), m_size.height());
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_depthBufferObject);
}
void BitmapTexture::clearIfNeeded()
{
if (!m_shouldClear)
return;
m_clipStack.reset(IntRect(IntPoint::zero(), m_size), ClipStack::YAxisMode::Default);
m_clipStack.applyIfNeeded();
glClearColor(0, 0, 0, 0);
glClearStencil(0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
m_shouldClear = false;
}
void BitmapTexture::createFboIfNeeded()
{
if (m_fbo)
return;
glGenFramebuffers(1, &m_fbo);
glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, id(), 0);
if (m_flags.contains(Flags::DepthBuffer))
initializeDepthBuffer();
m_shouldClear = true;
}
void BitmapTexture::bindAsSurface()
{
glBindTexture(GL_TEXTURE_2D, 0);
createFboIfNeeded();
glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
glViewport(0, 0, m_size.width(), m_size.height());
if (m_flags.contains(Flags::DepthBuffer))
glEnable(GL_DEPTH_TEST);
else
glDisable(GL_DEPTH_TEST);
clearIfNeeded();
m_clipStack.apply();
}
BitmapTexture::~BitmapTexture()
{
glDeleteTextures(1, &m_id);
if (m_fbo)
glDeleteFramebuffers(1, &m_fbo);
#if !USE(TEXMAP_DEPTH_STENCIL_BUFFER)
if (m_rbo)
glDeleteRenderbuffers(1, &m_rbo);
#endif
if (m_depthBufferObject)
glDeleteRenderbuffers(1, &m_depthBufferObject);
}
void BitmapTexture::copyFromExternalTexture(GLuint sourceTextureID)
{
copyFromExternalTexture(sourceTextureID, { 0, 0, m_size.width(), m_size.height() }, { });
}
void BitmapTexture::copyFromExternalTexture(GLuint sourceTextureID, const IntRect& targetRect, const IntSize& sourceOffset)
{
RELEASE_ASSERT(sourceOffset.width() + targetRect.width() <= m_size.width());
RELEASE_ASSERT(sourceOffset.height() + targetRect.height() <= m_size.height());
GLint boundTexture = 0;
GLint boundFramebuffer = 0;
GLint boundActiveTexture = 0;
glGetIntegerv(GL_TEXTURE_BINDING_2D, &boundTexture);
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &boundFramebuffer);
glGetIntegerv(GL_ACTIVE_TEXTURE, &boundActiveTexture);
glBindTexture(GL_TEXTURE_2D, sourceTextureID);
GLuint copyFbo = 0;
glGenFramebuffers(1, ©Fbo);
glBindFramebuffer(GL_FRAMEBUFFER, copyFbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, sourceTextureID, 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, id());
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, targetRect.x(), targetRect.y(), sourceOffset.width(), sourceOffset.height(), targetRect.width(), targetRect.height());
glBindTexture(GL_TEXTURE_2D, boundTexture);
glBindFramebuffer(GL_FRAMEBUFFER, boundFramebuffer);
glBindTexture(GL_TEXTURE_2D, boundTexture);
glActiveTexture(boundActiveTexture);
glDeleteFramebuffers(1, ©Fbo);
}
void BitmapTexture::copyFromExternalTexture(BitmapTexture& sourceTexture, const IntRect& sourceRect, const IntSize& destinationOffset)
{
copyFromExternalTexture(sourceTexture.id(), sourceRect, destinationOffset);
}
} // namespace WebCore
#endif // USE(TEXTURE_MAPPER)