gfx/thebes/gfxCachedTempSurface.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
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 "gfxCachedTempSurface.h"
michael@0 7 #include "gfxContext.h"
michael@0 8 #include "mozilla/Attributes.h"
michael@0 9
michael@0 10 class CachedSurfaceExpirationTracker MOZ_FINAL :
michael@0 11 public nsExpirationTracker<gfxCachedTempSurface,2> {
michael@0 12
michael@0 13 public:
michael@0 14 // With K = 2, this means that surfaces will be released when they are not
michael@0 15 // used for 1-2 seconds.
michael@0 16 enum { TIMEOUT_MS = 1000 };
michael@0 17 CachedSurfaceExpirationTracker()
michael@0 18 : nsExpirationTracker<gfxCachedTempSurface,2>(TIMEOUT_MS) {}
michael@0 19
michael@0 20 ~CachedSurfaceExpirationTracker() {
michael@0 21 AgeAllGenerations();
michael@0 22 }
michael@0 23
michael@0 24 virtual void NotifyExpired(gfxCachedTempSurface* aSurface) {
michael@0 25 RemoveObject(aSurface);
michael@0 26 aSurface->Expire();
michael@0 27 }
michael@0 28
michael@0 29 static void MarkSurfaceUsed(gfxCachedTempSurface* aSurface)
michael@0 30 {
michael@0 31 if (aSurface->GetExpirationState()->IsTracked()) {
michael@0 32 sExpirationTracker->MarkUsed(aSurface);
michael@0 33 return;
michael@0 34 }
michael@0 35
michael@0 36 if (!sExpirationTracker) {
michael@0 37 sExpirationTracker = new CachedSurfaceExpirationTracker();
michael@0 38 }
michael@0 39 sExpirationTracker->AddObject(aSurface);
michael@0 40 }
michael@0 41
michael@0 42 static void RemoveSurface(gfxCachedTempSurface* aSurface)
michael@0 43 {
michael@0 44 if (!sExpirationTracker)
michael@0 45 return;
michael@0 46
michael@0 47 if (aSurface->GetExpirationState()->IsTracked()) {
michael@0 48 sExpirationTracker->RemoveObject(aSurface);
michael@0 49 }
michael@0 50 if (sExpirationTracker->IsEmpty()) {
michael@0 51 delete sExpirationTracker;
michael@0 52 sExpirationTracker = nullptr;
michael@0 53 }
michael@0 54 }
michael@0 55
michael@0 56 private:
michael@0 57 static CachedSurfaceExpirationTracker* sExpirationTracker;
michael@0 58 };
michael@0 59
michael@0 60 CachedSurfaceExpirationTracker*
michael@0 61 CachedSurfaceExpirationTracker::sExpirationTracker = nullptr;
michael@0 62
michael@0 63 gfxCachedTempSurface::~gfxCachedTempSurface()
michael@0 64 {
michael@0 65 CachedSurfaceExpirationTracker::RemoveSurface(this);
michael@0 66 }
michael@0 67
michael@0 68 already_AddRefed<gfxContext>
michael@0 69 gfxCachedTempSurface::Get(gfxContentType aContentType,
michael@0 70 const gfxRect& aRect,
michael@0 71 gfxASurface* aSimilarTo)
michael@0 72 {
michael@0 73 if (mSurface) {
michael@0 74 /* Verify the current buffer is valid for this purpose */
michael@0 75 if (mSize.width < aRect.width || mSize.height < aRect.height
michael@0 76 || mSurface->GetContentType() != aContentType
michael@0 77 || mType != aSimilarTo->GetType()) {
michael@0 78 mSurface = nullptr;
michael@0 79 }
michael@0 80 }
michael@0 81
michael@0 82 bool cleared = false;
michael@0 83 if (!mSurface) {
michael@0 84 mSize = gfxIntSize(int32_t(ceil(aRect.width)), int32_t(ceil(aRect.height)));
michael@0 85 mSurface = aSimilarTo->CreateSimilarSurface(aContentType, mSize);
michael@0 86 if (!mSurface)
michael@0 87 return nullptr;
michael@0 88
michael@0 89 cleared = true;
michael@0 90 mType = aSimilarTo->GetType();
michael@0 91 }
michael@0 92 mSurface->SetDeviceOffset(-aRect.TopLeft());
michael@0 93
michael@0 94 nsRefPtr<gfxContext> ctx = new gfxContext(mSurface);
michael@0 95 ctx->Rectangle(aRect);
michael@0 96 ctx->Clip();
michael@0 97 if (!cleared && aContentType != gfxContentType::COLOR) {
michael@0 98 ctx->SetOperator(gfxContext::OPERATOR_CLEAR);
michael@0 99 ctx->Paint();
michael@0 100 ctx->SetOperator(gfxContext::OPERATOR_OVER);
michael@0 101 }
michael@0 102
michael@0 103 CachedSurfaceExpirationTracker::MarkSurfaceUsed(this);
michael@0 104
michael@0 105 return ctx.forget();
michael@0 106 }

mercurial