| OLD | NEW |
| 1 // Copyright 2011 The Chromium Authors. All rights reserved. | 1 // Copyright 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "cc/test/fake_web_graphics_context_3d.h" | 5 #include "cc/test/fake_web_graphics_context_3d.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> |
| 8 | 9 |
| 9 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "third_party/khronos/GLES2/gl2ext.h" | 12 #include "third_party/khronos/GLES2/gl2ext.h" |
| 11 | 13 |
| 12 using WebKit::WGC3Dboolean; | 14 using WebKit::WGC3Dboolean; |
| 13 using WebKit::WGC3Denum; | 15 using WebKit::WGC3Denum; |
| 14 using WebKit::WebGLId; | 16 using WebKit::WebGLId; |
| 15 using WebKit::WebGraphicsContext3D; | 17 using WebKit::WebGraphicsContext3D; |
| 16 | 18 |
| 17 namespace cc { | 19 namespace cc { |
| 18 | 20 |
| 21 static const WebGLId kBufferId = 1; |
| 22 static const WebGLId kFramebufferId = 2; |
| 23 static const WebGLId kProgramId = 3; |
| 24 static const WebGLId kRenderbufferId = 4; |
| 25 static const WebGLId kShaderId = 5; |
| 26 |
| 27 static unsigned s_context_id = 1; |
| 28 |
| 29 WebKit::WebGLId FakeWebGraphicsContext3D::kExternalTextureId = 1337; |
| 30 |
| 19 FakeWebGraphicsContext3D::FakeWebGraphicsContext3D() | 31 FakeWebGraphicsContext3D::FakeWebGraphicsContext3D() |
| 20 : next_texture_id_(1) | 32 : context_id_(s_context_id++), |
| 21 , context_lost_(false) | 33 next_texture_id_(1), |
| 22 , context_lost_callback_(NULL) | 34 have_extension_io_surface_(false), |
| 23 { | 35 have_extension_egl_image_(false), |
| 36 times_make_current_succeeds_(-1), |
| 37 times_bind_texture_succeeds_(-1), |
| 38 times_end_query_succeeds_(-1), |
| 39 context_lost_(false), |
| 40 context_lost_callback_(NULL), |
| 41 width_(0), |
| 42 height_(0) { |
| 24 } | 43 } |
| 25 | 44 |
| 26 FakeWebGraphicsContext3D::FakeWebGraphicsContext3D( | 45 FakeWebGraphicsContext3D::FakeWebGraphicsContext3D( |
| 27 const WebGraphicsContext3D::Attributes& attributes) | 46 const WebGraphicsContext3D::Attributes& attributes) |
| 28 : next_texture_id_(1) | 47 : context_id_(s_context_id++), |
| 29 , attributes_(attributes) | 48 next_texture_id_(1), |
| 30 , context_lost_(false) | 49 attributes_(attributes), |
| 31 , context_lost_callback_(NULL) | 50 have_extension_io_surface_(false), |
| 32 { | 51 have_extension_egl_image_(false), |
| 52 times_make_current_succeeds_(-1), |
| 53 times_bind_texture_succeeds_(-1), |
| 54 times_end_query_succeeds_(-1), |
| 55 context_lost_(false), |
| 56 context_lost_callback_(NULL), |
| 57 width_(0), |
| 58 height_(0) { |
| 33 } | 59 } |
| 34 | 60 |
| 35 FakeWebGraphicsContext3D::~FakeWebGraphicsContext3D() | 61 FakeWebGraphicsContext3D::~FakeWebGraphicsContext3D() { |
| 36 { | |
| 37 } | 62 } |
| 38 | 63 |
| 39 bool FakeWebGraphicsContext3D::makeContextCurrent() { | 64 bool FakeWebGraphicsContext3D::makeContextCurrent() { |
| 65 if (times_make_current_succeeds_ >= 0) { |
| 66 if (!times_make_current_succeeds_) |
| 67 loseContextCHROMIUM(); |
| 68 --times_make_current_succeeds_; |
| 69 } |
| 40 return !context_lost_; | 70 return !context_lost_; |
| 41 } | 71 } |
| 42 | 72 |
| 43 int FakeWebGraphicsContext3D::width() { | 73 int FakeWebGraphicsContext3D::width() { |
| 44 return 0; | 74 return width_; |
| 45 } | 75 } |
| 46 | 76 |
| 47 int FakeWebGraphicsContext3D::height() { | 77 int FakeWebGraphicsContext3D::height() { |
| 48 return 0; | 78 return height_; |
| 79 } |
| 80 |
| 81 void FakeWebGraphicsContext3D::reshape(int width, int height) { |
| 82 width_ = width; |
| 83 height_ = height; |
| 49 } | 84 } |
| 50 | 85 |
| 51 bool FakeWebGraphicsContext3D::isGLES2Compliant() { | 86 bool FakeWebGraphicsContext3D::isGLES2Compliant() { |
| 52 return false; | 87 return false; |
| 53 } | 88 } |
| 54 | 89 |
| 55 bool FakeWebGraphicsContext3D::readBackFramebuffer( | 90 bool FakeWebGraphicsContext3D::readBackFramebuffer( |
| 56 unsigned char* pixels, | 91 unsigned char* pixels, |
| 57 size_t bufferSize, | 92 size_t bufferSize, |
| 58 WebGLId framebuffer, | 93 WebGLId framebuffer, |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 WebGLId shader) { | 201 WebGLId shader) { |
| 167 return WebKit::WebString(); | 202 return WebKit::WebString(); |
| 168 } | 203 } |
| 169 | 204 |
| 170 WebKit::WebString FakeWebGraphicsContext3D::getShaderSource( | 205 WebKit::WebString FakeWebGraphicsContext3D::getShaderSource( |
| 171 WebGLId shader) { | 206 WebGLId shader) { |
| 172 return WebKit::WebString(); | 207 return WebKit::WebString(); |
| 173 } | 208 } |
| 174 | 209 |
| 175 WebKit::WebString FakeWebGraphicsContext3D::getString(WGC3Denum name) { | 210 WebKit::WebString FakeWebGraphicsContext3D::getString(WGC3Denum name) { |
| 176 return WebKit::WebString(); | 211 std::string string; |
| 212 |
| 213 if (name == GL_EXTENSIONS) { |
| 214 if (have_extension_io_surface_) |
| 215 string += "GL_CHROMIUM_iosurface GL_ARB_texture_rectangle "; |
| 216 if (have_extension_egl_image_) |
| 217 string += "GL_OES_EGL_image_external"; |
| 218 } |
| 219 |
| 220 return WebKit::WebString::fromUTF8(string.c_str()); |
| 177 } | 221 } |
| 178 | 222 |
| 179 WebKit::WGC3Dint FakeWebGraphicsContext3D::getUniformLocation( | 223 WebKit::WGC3Dint FakeWebGraphicsContext3D::getUniformLocation( |
| 180 WebGLId program, | 224 WebGLId program, |
| 181 const WebKit::WGC3Dchar* name) { | 225 const WebKit::WGC3Dchar* name) { |
| 182 return 0; | 226 return 0; |
| 183 } | 227 } |
| 184 | 228 |
| 185 WebKit::WGC3Dsizeiptr FakeWebGraphicsContext3D::getVertexAttribOffset( | 229 WebKit::WGC3Dsizeiptr FakeWebGraphicsContext3D::getVertexAttribOffset( |
| 186 WebKit::WGC3Duint index, | 230 WebKit::WGC3Duint index, |
| (...skipping 30 matching lines...) Expand all Loading... |
| 217 WebGLId shader) { | 261 WebGLId shader) { |
| 218 return false; | 262 return false; |
| 219 } | 263 } |
| 220 | 264 |
| 221 WGC3Dboolean FakeWebGraphicsContext3D::isTexture( | 265 WGC3Dboolean FakeWebGraphicsContext3D::isTexture( |
| 222 WebGLId texture) { | 266 WebGLId texture) { |
| 223 return false; | 267 return false; |
| 224 } | 268 } |
| 225 | 269 |
| 226 WebGLId FakeWebGraphicsContext3D::createBuffer() { | 270 WebGLId FakeWebGraphicsContext3D::createBuffer() { |
| 227 return 1; | 271 return kBufferId | context_id_ << 16; |
| 272 } |
| 273 |
| 274 void FakeWebGraphicsContext3D::deleteBuffer(WebKit::WebGLId id) { |
| 275 EXPECT_EQ(kBufferId | context_id_ << 16, id); |
| 228 } | 276 } |
| 229 | 277 |
| 230 WebGLId FakeWebGraphicsContext3D::createFramebuffer() { | 278 WebGLId FakeWebGraphicsContext3D::createFramebuffer() { |
| 231 return 1; | 279 return kFramebufferId | context_id_ << 16; |
| 280 } |
| 281 |
| 282 void FakeWebGraphicsContext3D::deleteFramebuffer(WebKit::WebGLId id) { |
| 283 EXPECT_EQ(kFramebufferId | context_id_ << 16, id); |
| 232 } | 284 } |
| 233 | 285 |
| 234 WebGLId FakeWebGraphicsContext3D::createProgram() { | 286 WebGLId FakeWebGraphicsContext3D::createProgram() { |
| 235 return 1; | 287 return kProgramId | context_id_ << 16; |
| 288 } |
| 289 |
| 290 void FakeWebGraphicsContext3D::deleteProgram(WebKit::WebGLId id) { |
| 291 EXPECT_EQ(kProgramId | context_id_ << 16, id); |
| 236 } | 292 } |
| 237 | 293 |
| 238 WebGLId FakeWebGraphicsContext3D::createRenderbuffer() { | 294 WebGLId FakeWebGraphicsContext3D::createRenderbuffer() { |
| 239 return 1; | 295 return kRenderbufferId | context_id_ << 16; |
| 296 } |
| 297 |
| 298 void FakeWebGraphicsContext3D::deleteRenderbuffer(WebKit::WebGLId id) { |
| 299 EXPECT_EQ(kRenderbufferId | context_id_ << 16, id); |
| 240 } | 300 } |
| 241 | 301 |
| 242 WebGLId FakeWebGraphicsContext3D::createShader(WGC3Denum) { | 302 WebGLId FakeWebGraphicsContext3D::createShader(WGC3Denum) { |
| 243 return 1; | 303 return kShaderId | context_id_ << 16; |
| 304 } |
| 305 |
| 306 void FakeWebGraphicsContext3D::deleteShader(WebKit::WebGLId id) { |
| 307 EXPECT_EQ(kShaderId | context_id_ << 16, id); |
| 244 } | 308 } |
| 245 | 309 |
| 246 WebGLId FakeWebGraphicsContext3D::createTexture() { | 310 WebGLId FakeWebGraphicsContext3D::createTexture() { |
| 247 WebGLId texture_id = next_texture_id_++; | 311 WebGLId texture_id = NextTextureId(); |
| 312 DCHECK_NE(texture_id, kExternalTextureId); |
| 248 textures_.push_back(texture_id); | 313 textures_.push_back(texture_id); |
| 249 return texture_id; | 314 return texture_id; |
| 250 } | 315 } |
| 251 | 316 |
| 252 void FakeWebGraphicsContext3D::deleteTexture(WebGLId texture_id) { | 317 void FakeWebGraphicsContext3D::deleteTexture(WebGLId texture_id) { |
| 318 DCHECK(std::find(textures_.begin(), textures_.end(), texture_id) != |
| 319 textures_.end()); |
| 253 textures_.erase(std::find(textures_.begin(), textures_.end(), texture_id)); | 320 textures_.erase(std::find(textures_.begin(), textures_.end(), texture_id)); |
| 254 } | 321 } |
| 255 | 322 |
| 323 void FakeWebGraphicsContext3D::attachShader(WebGLId program, WebGLId shader) { |
| 324 EXPECT_EQ(kProgramId | context_id_ << 16, program); |
| 325 EXPECT_EQ(kShaderId | context_id_ << 16, shader); |
| 326 } |
| 327 |
| 328 void FakeWebGraphicsContext3D::useProgram(WebGLId program) { |
| 329 if (!program) |
| 330 return; |
| 331 EXPECT_EQ(kProgramId | context_id_ << 16, program); |
| 332 } |
| 333 |
| 334 void FakeWebGraphicsContext3D::bindBuffer(WGC3Denum target, WebGLId buffer) { |
| 335 if (!buffer) |
| 336 return; |
| 337 EXPECT_EQ(kBufferId | context_id_ << 16, buffer); |
| 338 } |
| 339 |
| 340 void FakeWebGraphicsContext3D::bindFramebuffer( |
| 341 WGC3Denum target, WebGLId framebuffer) { |
| 342 if (!framebuffer) |
| 343 return; |
| 344 EXPECT_EQ(kFramebufferId | context_id_ << 16, framebuffer); |
| 345 } |
| 346 |
| 347 void FakeWebGraphicsContext3D::bindRenderbuffer( |
| 348 WGC3Denum target, WebGLId renderbuffer) { |
| 349 if (!renderbuffer) |
| 350 return; |
| 351 EXPECT_EQ(kRenderbufferId | context_id_ << 16, renderbuffer); |
| 352 } |
| 353 |
| 256 void FakeWebGraphicsContext3D::bindTexture( | 354 void FakeWebGraphicsContext3D::bindTexture( |
| 257 WGC3Denum target, WebGLId texture_id) { | 355 WGC3Denum target, WebGLId texture_id) { |
| 356 if (times_bind_texture_succeeds_ >= 0) { |
| 357 if (!times_bind_texture_succeeds_) |
| 358 loseContextCHROMIUM(); |
| 359 --times_bind_texture_succeeds_; |
| 360 } |
| 361 |
| 258 if (!texture_id) | 362 if (!texture_id) |
| 259 return; | 363 return; |
| 364 if (texture_id == kExternalTextureId) |
| 365 return; |
| 260 DCHECK(std::find(textures_.begin(), textures_.end(), texture_id) != | 366 DCHECK(std::find(textures_.begin(), textures_.end(), texture_id) != |
| 261 textures_.end()); | 367 textures_.end()); |
| 262 used_textures_.insert(texture_id); | 368 used_textures_.insert(texture_id); |
| 263 } | 369 } |
| 264 | 370 |
| 265 WebGLId FakeWebGraphicsContext3D::createQueryEXT() { | 371 WebGLId FakeWebGraphicsContext3D::createQueryEXT() { |
| 266 return 1; | 372 return 1; |
| 267 } | 373 } |
| 268 | 374 |
| 269 WGC3Dboolean FakeWebGraphicsContext3D::isQueryEXT(WebGLId query) { | 375 WGC3Dboolean FakeWebGraphicsContext3D::isQueryEXT(WebGLId query) { |
| 270 return true; | 376 return true; |
| 271 } | 377 } |
| 272 | 378 |
| 273 void FakeWebGraphicsContext3D::SetContextLostCallback( | 379 void FakeWebGraphicsContext3D::endQueryEXT(WebKit::WGC3Denum target) { |
| 380 if (times_end_query_succeeds_ >= 0) { |
| 381 if (!times_end_query_succeeds_) |
| 382 loseContextCHROMIUM(); |
| 383 --times_end_query_succeeds_; |
| 384 } |
| 385 } |
| 386 |
| 387 void FakeWebGraphicsContext3D::getQueryObjectuivEXT( |
| 388 WebKit::WebGLId query, |
| 389 WebKit::WGC3Denum pname, |
| 390 WebKit::WGC3Duint* params) { |
| 391 // If the context is lost, behave as if result is available. |
| 392 if (pname == GL_QUERY_RESULT_AVAILABLE_EXT) |
| 393 *params = 1; |
| 394 } |
| 395 |
| 396 void FakeWebGraphicsContext3D::setContextLostCallback( |
| 274 WebGraphicsContextLostCallback* callback) { | 397 WebGraphicsContextLostCallback* callback) { |
| 275 context_lost_callback_ = callback; | 398 context_lost_callback_ = callback; |
| 276 } | 399 } |
| 277 | 400 |
| 278 void FakeWebGraphicsContext3D::loseContextCHROMIUM() { | 401 void FakeWebGraphicsContext3D::loseContextCHROMIUM() { |
| 402 if (context_lost_) |
| 403 return; |
| 279 context_lost_ = true; | 404 context_lost_ = true; |
| 280 if (context_lost_callback_) | 405 if (context_lost_callback_) |
| 281 context_lost_callback_->onContextLost(); | 406 context_lost_callback_->onContextLost(); |
| 282 } | 407 } |
| 283 | 408 |
| 409 WebKit::WebGLId FakeWebGraphicsContext3D::NextTextureId() { |
| 410 WebGLId texture_id = next_texture_id_++; |
| 411 DCHECK(texture_id < (1 << 16)); |
| 412 texture_id |= context_id_ << 16; |
| 413 return texture_id; |
| 414 } |
| 415 |
| 284 } // namespace cc | 416 } // namespace cc |
| OLD | NEW |