gfx/gl/SurfaceStream.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* -*- Mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40; -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "SurfaceStream.h"
michael@0 7
michael@0 8 #include "gfxPoint.h"
michael@0 9 #include "SharedSurface.h"
michael@0 10 #include "SharedSurfaceGL.h"
michael@0 11 #include "SurfaceFactory.h"
michael@0 12 #include "GeckoProfiler.h"
michael@0 13
michael@0 14 namespace mozilla {
michael@0 15 namespace gfx {
michael@0 16
michael@0 17 SurfaceStreamType
michael@0 18 SurfaceStream::ChooseGLStreamType(SurfaceStream::OMTC omtc,
michael@0 19 bool preserveBuffer)
michael@0 20 {
michael@0 21 if (omtc == SurfaceStream::OffMainThread) {
michael@0 22 if (preserveBuffer)
michael@0 23 return SurfaceStreamType::TripleBuffer_Copy;
michael@0 24 else
michael@0 25 return SurfaceStreamType::TripleBuffer_Async;
michael@0 26 } else {
michael@0 27 if (preserveBuffer)
michael@0 28 return SurfaceStreamType::SingleBuffer;
michael@0 29 else
michael@0 30 return SurfaceStreamType::TripleBuffer;
michael@0 31 }
michael@0 32 }
michael@0 33
michael@0 34 SurfaceStream*
michael@0 35 SurfaceStream::CreateForType(SurfaceStreamType type, mozilla::gl::GLContext* glContext, SurfaceStream* prevStream)
michael@0 36 {
michael@0 37 SurfaceStream* result = nullptr;
michael@0 38
michael@0 39 switch (type) {
michael@0 40 case SurfaceStreamType::SingleBuffer:
michael@0 41 result = new SurfaceStream_SingleBuffer(prevStream);
michael@0 42 break;
michael@0 43 case SurfaceStreamType::TripleBuffer_Copy:
michael@0 44 result = new SurfaceStream_TripleBuffer_Copy(prevStream);
michael@0 45 break;
michael@0 46 case SurfaceStreamType::TripleBuffer_Async:
michael@0 47 result = new SurfaceStream_TripleBuffer_Async(prevStream);
michael@0 48 break;
michael@0 49 case SurfaceStreamType::TripleBuffer:
michael@0 50 result = new SurfaceStream_TripleBuffer(prevStream);
michael@0 51 break;
michael@0 52 default:
michael@0 53 MOZ_CRASH("Invalid Type.");
michael@0 54 }
michael@0 55
michael@0 56 result->mGLContext = glContext;
michael@0 57 return result;
michael@0 58 }
michael@0 59
michael@0 60 bool
michael@0 61 SurfaceStream_TripleBuffer::CopySurfaceToProducer(SharedSurface* src, SurfaceFactory* factory)
michael@0 62 {
michael@0 63 if (!mProducer) {
michael@0 64 New(factory, src->Size(), mProducer);
michael@0 65 if (!mProducer) {
michael@0 66 return false;
michael@0 67 }
michael@0 68 }
michael@0 69
michael@0 70 MOZ_ASSERT(src->Size() == mProducer->Size(), "Size mismatch");
michael@0 71
michael@0 72 SharedSurface::Copy(src, mProducer, factory);
michael@0 73 return true;
michael@0 74 }
michael@0 75
michael@0 76 void
michael@0 77 SurfaceStream::New(SurfaceFactory* factory, const gfx::IntSize& size,
michael@0 78 SharedSurface*& surf)
michael@0 79 {
michael@0 80 MOZ_ASSERT(!surf);
michael@0 81 surf = factory->NewSharedSurface(size);
michael@0 82
michael@0 83 if (surf)
michael@0 84 mSurfaces.insert(surf);
michael@0 85 }
michael@0 86
michael@0 87 void
michael@0 88 SurfaceStream::Recycle(SurfaceFactory* factory, SharedSurface*& surf)
michael@0 89 {
michael@0 90 if (surf) {
michael@0 91 mSurfaces.erase(surf);
michael@0 92 factory->Recycle(surf);
michael@0 93 }
michael@0 94 MOZ_ASSERT(!surf);
michael@0 95 }
michael@0 96
michael@0 97 void
michael@0 98 SurfaceStream::Delete(SharedSurface*& surf)
michael@0 99 {
michael@0 100 if (surf) {
michael@0 101 mSurfaces.erase(surf);
michael@0 102 delete surf;
michael@0 103 surf = nullptr;
michael@0 104 }
michael@0 105 MOZ_ASSERT(!surf);
michael@0 106 }
michael@0 107
michael@0 108 SharedSurface*
michael@0 109 SurfaceStream::Surrender(SharedSurface*& surf)
michael@0 110 {
michael@0 111 SharedSurface* ret = surf;
michael@0 112
michael@0 113 if (surf) {
michael@0 114 mSurfaces.erase(surf);
michael@0 115 surf = nullptr;
michael@0 116 }
michael@0 117 MOZ_ASSERT(!surf);
michael@0 118
michael@0 119 return ret;
michael@0 120 }
michael@0 121
michael@0 122 SharedSurface*
michael@0 123 SurfaceStream::Absorb(SharedSurface*& surf)
michael@0 124 {
michael@0 125 SharedSurface* ret = surf;
michael@0 126
michael@0 127 if (surf) {
michael@0 128 mSurfaces.insert(surf);
michael@0 129 surf = nullptr;
michael@0 130 }
michael@0 131 MOZ_ASSERT(!surf);
michael@0 132
michael@0 133 return ret;
michael@0 134 }
michael@0 135
michael@0 136 void
michael@0 137 SurfaceStream::Scrap(SharedSurface*& scrap)
michael@0 138 {
michael@0 139 if (scrap) {
michael@0 140 mScraps.push(scrap);
michael@0 141 scrap = nullptr;
michael@0 142 }
michael@0 143 MOZ_ASSERT(!scrap);
michael@0 144 }
michael@0 145
michael@0 146 void
michael@0 147 SurfaceStream::RecycleScraps(SurfaceFactory* factory)
michael@0 148 {
michael@0 149 while (!mScraps.empty()) {
michael@0 150 SharedSurface* cur = mScraps.top();
michael@0 151 mScraps.pop();
michael@0 152
michael@0 153 Recycle(factory, cur);
michael@0 154 }
michael@0 155 }
michael@0 156
michael@0 157
michael@0 158
michael@0 159 SurfaceStream::~SurfaceStream()
michael@0 160 {
michael@0 161 Delete(mProducer);
michael@0 162
michael@0 163 while (!mScraps.empty()) {
michael@0 164 SharedSurface* cur = mScraps.top();
michael@0 165 mScraps.pop();
michael@0 166
michael@0 167 Delete(cur);
michael@0 168 }
michael@0 169
michael@0 170 MOZ_ASSERT(mSurfaces.empty());
michael@0 171 }
michael@0 172
michael@0 173 SharedSurface*
michael@0 174 SurfaceStream::SwapConsumer()
michael@0 175 {
michael@0 176 MOZ_ASSERT(mIsAlive);
michael@0 177
michael@0 178 SharedSurface* ret = SwapConsumer_NoWait();
michael@0 179 if (!ret)
michael@0 180 return nullptr;
michael@0 181
michael@0 182 if (!ret->WaitSync()) {
michael@0 183 return nullptr;
michael@0 184 }
michael@0 185
michael@0 186 return ret;
michael@0 187 }
michael@0 188
michael@0 189 SharedSurface*
michael@0 190 SurfaceStream::Resize(SurfaceFactory* factory, const gfx::IntSize& size)
michael@0 191 {
michael@0 192 MonitorAutoLock lock(mMonitor);
michael@0 193
michael@0 194 if (mProducer) {
michael@0 195 Scrap(mProducer);
michael@0 196 }
michael@0 197
michael@0 198 New(factory, size, mProducer);
michael@0 199 return mProducer;
michael@0 200 }
michael@0 201
michael@0 202 SurfaceStream_SingleBuffer::SurfaceStream_SingleBuffer(SurfaceStream* prevStream)
michael@0 203 : SurfaceStream(SurfaceStreamType::SingleBuffer, prevStream)
michael@0 204 , mConsumer(nullptr)
michael@0 205 {
michael@0 206 if (!prevStream)
michael@0 207 return;
michael@0 208
michael@0 209 SharedSurface* prevProducer = nullptr;
michael@0 210 SharedSurface* prevConsumer = nullptr;
michael@0 211 prevStream->SurrenderSurfaces(prevProducer, prevConsumer);
michael@0 212
michael@0 213 if (prevConsumer == prevProducer)
michael@0 214 prevConsumer = nullptr;
michael@0 215
michael@0 216 mProducer = Absorb(prevProducer);
michael@0 217 mConsumer = Absorb(prevConsumer);
michael@0 218 }
michael@0 219
michael@0 220 SurfaceStream_SingleBuffer::~SurfaceStream_SingleBuffer()
michael@0 221 {
michael@0 222 Delete(mConsumer);
michael@0 223 }
michael@0 224
michael@0 225 void
michael@0 226 SurfaceStream_SingleBuffer::SurrenderSurfaces(SharedSurface*& producer,
michael@0 227 SharedSurface*& consumer)
michael@0 228 {
michael@0 229 mIsAlive = false;
michael@0 230
michael@0 231 producer = Surrender(mProducer);
michael@0 232 consumer = Surrender(mConsumer);
michael@0 233
michael@0 234 if (!consumer)
michael@0 235 consumer = producer;
michael@0 236 }
michael@0 237
michael@0 238 SharedSurface*
michael@0 239 SurfaceStream_SingleBuffer::SwapProducer(SurfaceFactory* factory,
michael@0 240 const gfx::IntSize& size)
michael@0 241 {
michael@0 242 MonitorAutoLock lock(mMonitor);
michael@0 243 if (mConsumer) {
michael@0 244 Recycle(factory, mConsumer);
michael@0 245 }
michael@0 246
michael@0 247 if (mProducer) {
michael@0 248 // Fence now, before we start (maybe) juggling Prod around.
michael@0 249 mProducer->Fence();
michael@0 250
michael@0 251 // Size mismatch means we need to squirrel the current Prod
michael@0 252 // into Cons, and leave Prod empty, so it gets a new surface below.
michael@0 253 bool needsNewBuffer = mProducer->Size() != size;
michael@0 254
michael@0 255 // Even if we're the right size, if the type has changed, and we don't
michael@0 256 // need to preserve, we should switch out for (presumedly) better perf.
michael@0 257 if (mProducer->Type() != factory->Type() &&
michael@0 258 !factory->Caps().preserve)
michael@0 259 {
michael@0 260 needsNewBuffer = true;
michael@0 261 }
michael@0 262
michael@0 263 if (needsNewBuffer) {
michael@0 264 Move(mProducer, mConsumer);
michael@0 265 }
michael@0 266 }
michael@0 267
michael@0 268 // The old Prod (if there every was one) was invalid,
michael@0 269 // so we need a new one.
michael@0 270 if (!mProducer) {
michael@0 271 New(factory, size, mProducer);
michael@0 272 }
michael@0 273
michael@0 274 return mProducer;
michael@0 275 }
michael@0 276
michael@0 277 SharedSurface*
michael@0 278 SurfaceStream_SingleBuffer::SwapConsumer_NoWait()
michael@0 279 {
michael@0 280 MonitorAutoLock lock(mMonitor);
michael@0 281
michael@0 282 // Use Cons, if present.
michael@0 283 // Otherwise, just use Prod directly.
michael@0 284 SharedSurface* toConsume = mConsumer;
michael@0 285 if (!toConsume)
michael@0 286 toConsume = mProducer;
michael@0 287
michael@0 288 return toConsume;
michael@0 289 }
michael@0 290
michael@0 291
michael@0 292
michael@0 293 SurfaceStream_TripleBuffer_Copy::SurfaceStream_TripleBuffer_Copy(SurfaceStream* prevStream)
michael@0 294 : SurfaceStream(SurfaceStreamType::TripleBuffer_Copy, prevStream)
michael@0 295 , mStaging(nullptr)
michael@0 296 , mConsumer(nullptr)
michael@0 297 {
michael@0 298 if (!prevStream)
michael@0 299 return;
michael@0 300
michael@0 301 SharedSurface* prevProducer = nullptr;
michael@0 302 SharedSurface* prevConsumer = nullptr;
michael@0 303 prevStream->SurrenderSurfaces(prevProducer, prevConsumer);
michael@0 304
michael@0 305 if (prevConsumer == prevProducer)
michael@0 306 prevConsumer = nullptr;
michael@0 307
michael@0 308 mProducer = Absorb(prevProducer);
michael@0 309 mConsumer = Absorb(prevConsumer);
michael@0 310 }
michael@0 311
michael@0 312 SurfaceStream_TripleBuffer_Copy::~SurfaceStream_TripleBuffer_Copy()
michael@0 313 {
michael@0 314 Delete(mStaging);
michael@0 315 Delete(mConsumer);
michael@0 316 }
michael@0 317
michael@0 318 void
michael@0 319 SurfaceStream_TripleBuffer_Copy::SurrenderSurfaces(SharedSurface*& producer,
michael@0 320 SharedSurface*& consumer)
michael@0 321 {
michael@0 322 mIsAlive = false;
michael@0 323
michael@0 324 producer = Surrender(mProducer);
michael@0 325 consumer = Surrender(mConsumer);
michael@0 326
michael@0 327 if (!consumer)
michael@0 328 consumer = Surrender(mStaging);
michael@0 329 }
michael@0 330
michael@0 331 SharedSurface*
michael@0 332 SurfaceStream_TripleBuffer_Copy::SwapProducer(SurfaceFactory* factory,
michael@0 333 const gfx::IntSize& size)
michael@0 334 {
michael@0 335 MonitorAutoLock lock(mMonitor);
michael@0 336
michael@0 337 RecycleScraps(factory);
michael@0 338 if (mProducer) {
michael@0 339 if (mStaging) {
michael@0 340 // We'll re-use this for a new mProducer later on if
michael@0 341 // the size remains the same
michael@0 342 Recycle(factory, mStaging);
michael@0 343 }
michael@0 344
michael@0 345 Move(mProducer, mStaging);
michael@0 346 mStaging->Fence();
michael@0 347
michael@0 348 New(factory, size, mProducer);
michael@0 349
michael@0 350 if (mProducer && mStaging->Size() == mProducer->Size())
michael@0 351 SharedSurface::Copy(mStaging, mProducer, factory);
michael@0 352 } else {
michael@0 353 New(factory, size, mProducer);
michael@0 354 }
michael@0 355
michael@0 356 return mProducer;
michael@0 357 }
michael@0 358
michael@0 359
michael@0 360 SharedSurface*
michael@0 361 SurfaceStream_TripleBuffer_Copy::SwapConsumer_NoWait()
michael@0 362 {
michael@0 363 MonitorAutoLock lock(mMonitor);
michael@0 364
michael@0 365 if (mStaging) {
michael@0 366 Scrap(mConsumer);
michael@0 367 Move(mStaging, mConsumer);
michael@0 368 }
michael@0 369
michael@0 370 return mConsumer;
michael@0 371 }
michael@0 372
michael@0 373 void SurfaceStream_TripleBuffer::Init(SurfaceStream* prevStream)
michael@0 374 {
michael@0 375 if (!prevStream)
michael@0 376 return;
michael@0 377
michael@0 378 SharedSurface* prevProducer = nullptr;
michael@0 379 SharedSurface* prevConsumer = nullptr;
michael@0 380 prevStream->SurrenderSurfaces(prevProducer, prevConsumer);
michael@0 381
michael@0 382 if (prevConsumer == prevProducer)
michael@0 383 prevConsumer = nullptr;
michael@0 384
michael@0 385 mProducer = Absorb(prevProducer);
michael@0 386 mConsumer = Absorb(prevConsumer);
michael@0 387 }
michael@0 388
michael@0 389
michael@0 390 SurfaceStream_TripleBuffer::SurfaceStream_TripleBuffer(SurfaceStreamType type, SurfaceStream* prevStream)
michael@0 391 : SurfaceStream(type, prevStream)
michael@0 392 , mStaging(nullptr)
michael@0 393 , mConsumer(nullptr)
michael@0 394 {
michael@0 395 SurfaceStream_TripleBuffer::Init(prevStream);
michael@0 396 }
michael@0 397
michael@0 398 SurfaceStream_TripleBuffer::SurfaceStream_TripleBuffer(SurfaceStream* prevStream)
michael@0 399 : SurfaceStream(SurfaceStreamType::TripleBuffer, prevStream)
michael@0 400 , mStaging(nullptr)
michael@0 401 , mConsumer(nullptr)
michael@0 402 {
michael@0 403 SurfaceStream_TripleBuffer::Init(prevStream);
michael@0 404 }
michael@0 405
michael@0 406 SurfaceStream_TripleBuffer::~SurfaceStream_TripleBuffer()
michael@0 407 {
michael@0 408 Delete(mStaging);
michael@0 409 Delete(mConsumer);
michael@0 410 }
michael@0 411
michael@0 412 void
michael@0 413 SurfaceStream_TripleBuffer::SurrenderSurfaces(SharedSurface*& producer,
michael@0 414 SharedSurface*& consumer)
michael@0 415 {
michael@0 416 mIsAlive = false;
michael@0 417
michael@0 418 producer = Surrender(mProducer);
michael@0 419 consumer = Surrender(mConsumer);
michael@0 420
michael@0 421 if (!consumer)
michael@0 422 consumer = Surrender(mStaging);
michael@0 423 }
michael@0 424
michael@0 425 SharedSurface*
michael@0 426 SurfaceStream_TripleBuffer::SwapProducer(SurfaceFactory* factory,
michael@0 427 const gfx::IntSize& size)
michael@0 428 {
michael@0 429 PROFILER_LABEL("SurfaceStream_TripleBuffer", "SwapProducer");
michael@0 430
michael@0 431 MonitorAutoLock lock(mMonitor);
michael@0 432 if (mProducer) {
michael@0 433 RecycleScraps(factory);
michael@0 434
michael@0 435 // If WaitForCompositor succeeds, mStaging has moved to mConsumer.
michael@0 436 // If it failed, we might have to scrap it.
michael@0 437 if (mStaging && !WaitForCompositor())
michael@0 438 Scrap(mStaging);
michael@0 439
michael@0 440 MOZ_ASSERT(!mStaging);
michael@0 441 Move(mProducer, mStaging);
michael@0 442 mStaging->Fence();
michael@0 443 }
michael@0 444
michael@0 445 MOZ_ASSERT(!mProducer);
michael@0 446 New(factory, size, mProducer);
michael@0 447
michael@0 448 return mProducer;
michael@0 449 }
michael@0 450
michael@0 451 SharedSurface*
michael@0 452 SurfaceStream_TripleBuffer::SwapConsumer_NoWait()
michael@0 453 {
michael@0 454 MonitorAutoLock lock(mMonitor);
michael@0 455 if (mStaging) {
michael@0 456 Scrap(mConsumer);
michael@0 457 Move(mStaging, mConsumer);
michael@0 458 mMonitor.NotifyAll();
michael@0 459 }
michael@0 460
michael@0 461 return mConsumer;
michael@0 462 }
michael@0 463
michael@0 464 SurfaceStream_TripleBuffer_Async::SurfaceStream_TripleBuffer_Async(SurfaceStream* prevStream)
michael@0 465 : SurfaceStream_TripleBuffer(SurfaceStreamType::TripleBuffer_Async, prevStream)
michael@0 466 {
michael@0 467 }
michael@0 468
michael@0 469 SurfaceStream_TripleBuffer_Async::~SurfaceStream_TripleBuffer_Async()
michael@0 470 {
michael@0 471 }
michael@0 472
michael@0 473 bool
michael@0 474 SurfaceStream_TripleBuffer_Async::WaitForCompositor()
michael@0 475 {
michael@0 476 PROFILER_LABEL("SurfaceStream_TripleBuffer_Async", "WaitForCompositor");
michael@0 477
michael@0 478 // We are assumed to be locked
michael@0 479 while (mStaging) {
michael@0 480 if (!NS_SUCCEEDED(mMonitor.Wait(PR_MillisecondsToInterval(100)))) {
michael@0 481 return false;
michael@0 482 }
michael@0 483 }
michael@0 484
michael@0 485 return true;
michael@0 486 }
michael@0 487
michael@0 488 } /* namespace gfx */
michael@0 489 } /* namespace mozilla */

mercurial