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 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
|
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "gpu/command_buffer/common/capabilities.h"
#include "platform/RuntimeEnabledFeatures.h"
#include "platform/graphics/gpu/DrawingBuffer.h"
#include "platform/graphics/gpu/Extensions3DUtil.h"
#include "public/platform/WebGraphicsContext3DProvider.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace blink {
enum {
InitialWidth = 100,
InitialHeight = 100,
AlternateHeight = 50,
};
class DrawingBufferForTests : public DrawingBuffer {
public:
static PassRefPtr<DrawingBufferForTests> create(
std::unique_ptr<WebGraphicsContext3DProvider> contextProvider,
DrawingBuffer::Client* client,
const IntSize& size,
PreserveDrawingBuffer preserve) {
std::unique_ptr<Extensions3DUtil> extensionsUtil =
Extensions3DUtil::create(contextProvider->contextGL());
RefPtr<DrawingBufferForTests> drawingBuffer = adoptRef(
new DrawingBufferForTests(std::move(contextProvider),
std::move(extensionsUtil), client, preserve));
bool multisampleExtensionSupported = false;
if (!drawingBuffer->initialize(size, multisampleExtensionSupported)) {
drawingBuffer->beginDestruction();
return nullptr;
}
return drawingBuffer.release();
}
DrawingBufferForTests(
std::unique_ptr<WebGraphicsContext3DProvider> contextProvider,
std::unique_ptr<Extensions3DUtil> extensionsUtil,
DrawingBuffer::Client* client,
PreserveDrawingBuffer preserve)
: DrawingBuffer(
std::move(contextProvider),
std::move(extensionsUtil),
client,
false /* discardFramebufferSupported */,
true /* wantAlphaChannel */,
false /* premultipliedAlpha */,
preserve,
WebGL1,
false /* wantDepth */,
false /* wantStencil */,
DrawingBuffer::AllowChromiumImage /* ChromiumImageUsage */),
m_live(0) {}
~DrawingBufferForTests() override {
if (m_live)
*m_live = false;
}
bool* m_live;
int recycledBitmapCount() { return m_recycledBitmaps.size(); }
};
class WebGraphicsContext3DProviderForTests
: public WebGraphicsContext3DProvider {
public:
WebGraphicsContext3DProviderForTests(
std::unique_ptr<gpu::gles2::GLES2Interface> gl)
: m_gl(std::move(gl)) {}
gpu::gles2::GLES2Interface* contextGL() override { return m_gl.get(); }
bool isSoftwareRendering() const override { return false; }
// Not used by WebGL code.
GrContext* grContext() override { return nullptr; }
bool bindToCurrentThread() override { return false; }
gpu::Capabilities getCapabilities() override { return gpu::Capabilities(); }
void setLostContextCallback(const base::Closure&) {}
void setErrorMessageCallback(
const base::Callback<void(const char*, int32_t id)>&) {}
void signalQuery(uint32_t, const base::Closure&) override {}
private:
std::unique_ptr<gpu::gles2::GLES2Interface> m_gl;
};
// The target to use when binding a texture to a Chromium image.
GLenum imageCHROMIUMTextureTarget() {
#if OS(MACOSX)
return GC3D_TEXTURE_RECTANGLE_ARB;
#else
return GL_TEXTURE_2D;
#endif
}
// The target to use when preparing a mailbox texture.
GLenum drawingBufferTextureTarget() {
if (RuntimeEnabledFeatures::webGLImageChromiumEnabled())
return imageCHROMIUMTextureTarget();
return GL_TEXTURE_2D;
}
class GLES2InterfaceForTests : public gpu::gles2::GLES2InterfaceStub,
public DrawingBuffer::Client {
public:
// GLES2InterfaceStub implementation:
void BindTexture(GLenum target, GLuint texture) override {
if (target == GL_TEXTURE_2D)
m_state.activeTexture2DBinding = texture;
m_boundTextures[target] = texture;
}
void BindFramebuffer(GLenum target, GLuint framebuffer) override {
switch (target) {
case GL_FRAMEBUFFER:
m_state.drawFramebufferBinding = framebuffer;
m_state.readFramebufferBinding = framebuffer;
break;
case GL_DRAW_FRAMEBUFFER:
m_state.drawFramebufferBinding = framebuffer;
break;
case GL_READ_FRAMEBUFFER:
m_state.readFramebufferBinding = framebuffer;
break;
default:
break;
}
}
void BindRenderbuffer(GLenum target, GLuint renderbuffer) override {
m_state.renderbufferBinding = renderbuffer;
}
void Enable(GLenum cap) {
if (cap == GL_SCISSOR_TEST)
m_state.scissorEnabled = true;
}
void Disable(GLenum cap) {
if (cap == GL_SCISSOR_TEST)
m_state.scissorEnabled = false;
}
void ClearColor(GLfloat red,
GLfloat green,
GLfloat blue,
GLfloat alpha) override {
m_state.clearColor[0] = red;
m_state.clearColor[1] = green;
m_state.clearColor[2] = blue;
m_state.clearColor[3] = alpha;
}
void ClearDepthf(GLfloat depth) override { m_state.clearDepth = depth; }
void ClearStencil(GLint s) override { m_state.clearStencil = s; }
void ColorMask(GLboolean red,
GLboolean green,
GLboolean blue,
GLboolean alpha) override {
m_state.colorMask[0] = red;
m_state.colorMask[1] = green;
m_state.colorMask[2] = blue;
m_state.colorMask[3] = alpha;
}
void DepthMask(GLboolean flag) override { m_state.depthMask = flag; }
void StencilMask(GLuint mask) override { m_state.stencilMask = mask; }
void StencilMaskSeparate(GLenum face, GLuint mask) override {
if (face == GL_FRONT)
m_state.stencilMask = mask;
}
void PixelStorei(GLenum pname, GLint param) override {
if (pname == GL_PACK_ALIGNMENT)
m_state.packAlignment = param;
}
void BindBuffer(GLenum target, GLuint buffer) override {
if (target == GL_PIXEL_UNPACK_BUFFER)
m_state.pixelUnpackBufferBinding = buffer;
}
GLuint64 InsertFenceSyncCHROMIUM() override {
static GLuint64 syncPointGenerator = 0;
return ++syncPointGenerator;
}
void WaitSyncTokenCHROMIUM(const GLbyte* syncToken) override {
memcpy(&m_mostRecentlyWaitedSyncToken, syncToken,
sizeof(m_mostRecentlyWaitedSyncToken));
}
GLenum CheckFramebufferStatus(GLenum target) override {
return GL_FRAMEBUFFER_COMPLETE;
}
void GetIntegerv(GLenum pname, GLint* value) override {
if (pname == GL_MAX_TEXTURE_SIZE)
*value = 1024;
}
void GenMailboxCHROMIUM(GLbyte* mailbox) override {
++m_currentMailboxByte;
memset(mailbox, m_currentMailboxByte, GL_MAILBOX_SIZE_CHROMIUM);
}
void ProduceTextureDirectCHROMIUM(GLuint texture,
GLenum target,
const GLbyte* mailbox) override {
ASSERT_EQ(target, drawingBufferTextureTarget());
if (!m_createImageChromiumFail) {
ASSERT_TRUE(m_textureSizes.contains(texture));
m_mostRecentlyProducedSize = m_textureSizes.get(texture);
}
}
void TexImage2D(GLenum target,
GLint level,
GLint internalformat,
GLsizei width,
GLsizei height,
GLint border,
GLenum format,
GLenum type,
const void* pixels) override {
if (target == GL_TEXTURE_2D && !level) {
m_textureSizes.set(m_boundTextures[target], IntSize(width, height));
}
}
GLuint CreateGpuMemoryBufferImageCHROMIUM(GLsizei width,
GLsizei height,
GLenum internalformat,
GLenum usage) override {
if (m_createImageChromiumFail)
return 0;
m_imageSizes.set(m_currentImageId, IntSize(width, height));
return m_currentImageId++;
}
MOCK_METHOD1(DestroyImageMock, void(GLuint imageId));
void DestroyImageCHROMIUM(GLuint imageId) {
m_imageSizes.remove(imageId);
// No textures should be bound to this.
CHECK(m_imageToTextureMap.find(imageId) == m_imageToTextureMap.end());
m_imageSizes.remove(imageId);
DestroyImageMock(imageId);
}
MOCK_METHOD1(BindTexImage2DMock, void(GLint imageId));
void BindTexImage2DCHROMIUM(GLenum target, GLint imageId) {
if (target == imageCHROMIUMTextureTarget()) {
m_textureSizes.set(m_boundTextures[target],
m_imageSizes.find(imageId)->value);
m_imageToTextureMap.set(imageId, m_boundTextures[target]);
BindTexImage2DMock(imageId);
}
}
MOCK_METHOD1(ReleaseTexImage2DMock, void(GLint imageId));
void ReleaseTexImage2DCHROMIUM(GLenum target, GLint imageId) {
if (target == imageCHROMIUMTextureTarget()) {
m_imageSizes.set(m_currentImageId, IntSize());
m_imageToTextureMap.remove(imageId);
ReleaseTexImage2DMock(imageId);
}
}
void GenSyncTokenCHROMIUM(GLuint64 fenceSync, GLbyte* syncToken) override {
static uint64_t uniqueId = 1;
gpu::SyncToken source(gpu::GPU_IO, 1,
gpu::CommandBufferId::FromUnsafeValue(uniqueId++), 2);
memcpy(syncToken, &source, sizeof(source));
}
void GenTextures(GLsizei n, GLuint* textures) override {
static GLuint id = 1;
for (GLsizei i = 0; i < n; ++i)
textures[i] = id++;
}
// DrawingBuffer::Client implementation.
bool DrawingBufferClientIsBoundForDraw() override {
return !m_state.drawFramebufferBinding;
}
void DrawingBufferClientRestoreScissorTest() override {
m_state.scissorEnabled = m_savedState.scissorEnabled;
}
void DrawingBufferClientRestoreMaskAndClearValues() override {
memcpy(m_state.colorMask, m_savedState.colorMask,
sizeof(m_state.colorMask));
m_state.clearDepth = m_savedState.clearDepth;
m_state.clearStencil = m_savedState.clearStencil;
memcpy(m_state.clearColor, m_savedState.clearColor,
sizeof(m_state.clearColor));
m_state.depthMask = m_savedState.depthMask;
m_state.stencilMask = m_savedState.stencilMask;
}
void DrawingBufferClientRestorePixelPackAlignment() override {
m_state.packAlignment = m_savedState.packAlignment;
}
void DrawingBufferClientRestoreTexture2DBinding() override {
m_state.activeTexture2DBinding = m_savedState.activeTexture2DBinding;
}
void DrawingBufferClientRestoreRenderbufferBinding() override {
m_state.renderbufferBinding = m_savedState.renderbufferBinding;
}
void DrawingBufferClientRestoreFramebufferBinding() override {
m_state.drawFramebufferBinding = m_savedState.drawFramebufferBinding;
m_state.readFramebufferBinding = m_savedState.readFramebufferBinding;
}
void DrawingBufferClientRestorePixelUnpackBufferBinding() override {
m_state.pixelUnpackBufferBinding = m_savedState.pixelUnpackBufferBinding;
}
// Testing methods.
gpu::SyncToken mostRecentlyWaitedSyncToken() const {
return m_mostRecentlyWaitedSyncToken;
}
GLuint nextImageIdToBeCreated() const { return m_currentImageId; }
IntSize mostRecentlyProducedSize() const {
return m_mostRecentlyProducedSize;
}
void setCreateImageChromiumFail(bool fail) {
m_createImageChromiumFail = fail;
}
// Saves current GL state for later verification.
void SaveState() { m_savedState = m_state; }
void VerifyStateHasNotChangedSinceSave() const {
for (size_t i = 0; i < 4; ++i) {
EXPECT_EQ(m_state.clearColor[0], m_savedState.clearColor[0]);
EXPECT_EQ(m_state.colorMask[0], m_savedState.colorMask[0]);
}
EXPECT_EQ(m_state.clearDepth, m_savedState.clearDepth);
EXPECT_EQ(m_state.clearStencil, m_savedState.clearStencil);
EXPECT_EQ(m_state.depthMask, m_savedState.depthMask);
EXPECT_EQ(m_state.stencilMask, m_savedState.stencilMask);
EXPECT_EQ(m_state.packAlignment, m_savedState.packAlignment);
EXPECT_EQ(m_state.activeTexture2DBinding,
m_savedState.activeTexture2DBinding);
EXPECT_EQ(m_state.renderbufferBinding, m_savedState.renderbufferBinding);
EXPECT_EQ(m_state.drawFramebufferBinding,
m_savedState.drawFramebufferBinding);
EXPECT_EQ(m_state.readFramebufferBinding,
m_savedState.readFramebufferBinding);
EXPECT_EQ(m_state.pixelUnpackBufferBinding,
m_savedState.pixelUnpackBufferBinding);
}
private:
std::map<GLenum, GLuint> m_boundTextures;
// State tracked to verify that it is restored correctly.
struct State {
bool scissorEnabled = false;
GLfloat clearColor[4] = {0, 0, 0, 0};
GLfloat clearDepth = 0;
GLint clearStencil = 0;
GLboolean colorMask[4] = {0, 0, 0, 0};
GLboolean depthMask = 0;
GLuint stencilMask = 0;
GLint packAlignment = 4;
// The bound 2D texture for the active texture unit.
GLuint activeTexture2DBinding = 0;
GLuint renderbufferBinding = 0;
GLuint drawFramebufferBinding = 0;
GLuint readFramebufferBinding = 0;
GLuint pixelUnpackBufferBinding = 0;
};
State m_state;
State m_savedState;
gpu::SyncToken m_mostRecentlyWaitedSyncToken;
GLbyte m_currentMailboxByte = 0;
IntSize m_mostRecentlyProducedSize;
bool m_createImageChromiumFail = false;
GLuint m_currentImageId = 1;
HashMap<GLuint, IntSize> m_textureSizes;
HashMap<GLuint, IntSize> m_imageSizes;
HashMap<GLuint, GLuint> m_imageToTextureMap;
};
} // blink
|