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 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
|
/*
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 "BitmapTextureGL.h"
#if USE(TEXTURE_MAPPER_GL)
#include "FilterOperations.h"
#include "LengthFunctions.h"
#include "NativeImage.h"
#include "NotImplemented.h"
#include "TextureMapperShaderProgram.h"
#include "Timer.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
#endif
namespace WebCore {
BitmapTextureGL* toBitmapTextureGL(BitmapTexture* texture)
{
if (!texture || !texture->isBackedByOpenGL())
return 0;
return static_cast<BitmapTextureGL*>(texture);
}
BitmapTextureGL::BitmapTextureGL(const TextureMapperContextAttributes& contextAttributes, const Flags, GLint internalFormat)
: m_contextAttributes(contextAttributes)
, m_format(GL_RGBA)
{
if (internalFormat != GL_DONT_CARE) {
m_internalFormat = internalFormat;
return;
}
m_internalFormat = GL_RGBA;
}
void BitmapTextureGL::didReset()
{
if (!m_id)
glGenTextures(1, &m_id);
m_shouldClear = true;
m_colorConvertFlags = TextureMapperGL::NoFlag;
m_filterInfo = FilterInfo();
if (m_textureSize == contentSize())
return;
m_textureSize = contentSize();
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_textureSize.width(), m_textureSize.height(), 0, m_format, m_type, 0);
}
void BitmapTextureGL::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.
m_colorConvertFlags = TextureMapperGL::ShouldConvertTextureBGRAToRGBA;
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 requireSubImageBuffer = !m_contextAttributes.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 (m_contextAttributes.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, m_type, data);
if (m_contextAttributes.supportsUnpackSubimage) {
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
}
}
void BitmapTextureGL::updateContents(Image* image, const IntRect& targetRect, const IntPoint& offset)
{
if (!image)
return;
auto frameImage = image->nativeImageForCurrentFrame();
if (!frameImage)
return;
int bytesPerLine;
const uint8_t* imageData;
#if USE(CAIRO)
cairo_surface_t* surface = frameImage->platformImage().get();
imageData = cairo_image_surface_get_data(surface);
bytesPerLine = cairo_image_surface_get_stride(surface);
#endif
updateContents(imageData, targetRect, offset, bytesPerLine);
}
static unsigned getPassesRequiredForFilter(FilterOperation::Type type)
{
switch (type) {
case FilterOperation::Type::Grayscale:
case FilterOperation::Type::Sepia:
case FilterOperation::Type::Saturate:
case FilterOperation::Type::HueRotate:
case FilterOperation::Type::Invert:
case FilterOperation::Type::Brightness:
case FilterOperation::Type::Contrast:
case FilterOperation::Type::Opacity:
return 1;
case FilterOperation::Type::Blur:
case FilterOperation::Type::DropShadow:
// We use two-passes (vertical+horizontal) for blur and drop-shadow.
return 2;
default:
return 0;
}
}
RefPtr<BitmapTexture> BitmapTextureGL::applyFilters(TextureMapper& textureMapper, const FilterOperations& filters, bool defersLastFilter)
{
if (filters.isEmpty())
return this;
TextureMapperGL& texmapGL = static_cast<TextureMapperGL&>(textureMapper);
RefPtr<BitmapTexture> previousSurface = texmapGL.currentSurface();
RefPtr<BitmapTexture> resultSurface = this;
RefPtr<BitmapTexture> intermediateSurface;
RefPtr<BitmapTexture> spareSurface;
m_filterInfo = FilterInfo();
for (size_t i = 0; i < filters.size(); ++i) {
RefPtr<FilterOperation> filter = filters.operations()[i];
ASSERT(filter);
int numPasses = getPassesRequiredForFilter(filter->type());
for (int j = 0; j < numPasses; ++j) {
bool last = (i == filters.size() - 1) && (j == numPasses - 1);
if (defersLastFilter && last) {
toBitmapTextureGL(resultSurface.get())->m_filterInfo = BitmapTextureGL::FilterInfo(filter.copyRef(), j, spareSurface.copyRef());
break;
}
if (!intermediateSurface)
intermediateSurface = texmapGL.acquireTextureFromPool(contentSize(), BitmapTexture::SupportsAlpha);
texmapGL.bindSurface(intermediateSurface.get());
texmapGL.drawFiltered(*resultSurface.get(), spareSurface.get(), *filter, j);
if (!j && filter->type() == FilterOperation::Type::DropShadow) {
spareSurface = resultSurface;
resultSurface = nullptr;
}
std::swap(resultSurface, intermediateSurface);
}
}
texmapGL.bindSurface(previousSurface.get());
return resultSurface;
}
void BitmapTextureGL::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_textureSize.width(), m_textureSize.height());
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, m_rbo);
glClearStencil(0);
glClear(GL_STENCIL_BUFFER_BIT);
#endif
}
void BitmapTextureGL::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_textureSize.width(), m_textureSize.height());
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_depthBufferObject);
}
void BitmapTextureGL::clearIfNeeded()
{
if (!m_shouldClear)
return;
m_clipStack.reset(IntRect(IntPoint::zero(), m_textureSize), 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 BitmapTextureGL::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 (flags() & DepthBuffer)
initializeDepthBuffer();
m_shouldClear = true;
}
void BitmapTextureGL::bindAsSurface()
{
glBindTexture(GL_TEXTURE_2D, 0);
createFboIfNeeded();
glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
glViewport(0, 0, m_textureSize.width(), m_textureSize.height());
if (flags() & DepthBuffer)
glEnable(GL_DEPTH_TEST);
else
glDisable(GL_DEPTH_TEST);
clearIfNeeded();
m_clipStack.apply();
}
BitmapTextureGL::~BitmapTextureGL()
{
if (m_id)
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);
}
bool BitmapTextureGL::isValid() const
{
return m_id;
}
IntSize BitmapTextureGL::size() const
{
return m_textureSize;
}
void BitmapTextureGL::copyFromExternalTexture(GLuint sourceTextureID)
{
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, 0, 0, 0, 0, m_textureSize.width(), m_textureSize.height());
glBindTexture(GL_TEXTURE_2D, boundTexture);
glBindFramebuffer(GL_FRAMEBUFFER, boundFramebuffer);
glBindTexture(GL_TEXTURE_2D, boundTexture);
glActiveTexture(boundActiveTexture);
glDeleteFramebuffers(1, ©Fbo);
}
} // namespace WebCore
#endif // USE(TEXTURE_MAPPER_GL)
|