Bug 835381 - Update libnestegg to 38c83d9d4c0c5c84373aa285bd30094a12d6b6f6. r=kinetik
[gecko.git] / gfx / thebes / gfxCachedTempSurface.cpp
blob43e62ced38a7de1a67446cdf9afb0a7fdd41100e
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "gfxCachedTempSurface.h"
7 #include "gfxContext.h"
8 #include "mozilla/Attributes.h"
10 class CachedSurfaceExpirationTracker MOZ_FINAL :
11 public nsExpirationTracker<gfxCachedTempSurface,2> {
13 public:
14 // With K = 2, this means that surfaces will be released when they are not
15 // used for 1-2 seconds.
16 enum { TIMEOUT_MS = 1000 };
17 CachedSurfaceExpirationTracker()
18 : nsExpirationTracker<gfxCachedTempSurface,2>(TIMEOUT_MS) {}
20 ~CachedSurfaceExpirationTracker() {
21 AgeAllGenerations();
24 virtual void NotifyExpired(gfxCachedTempSurface* aSurface) {
25 RemoveObject(aSurface);
26 aSurface->Expire();
29 static void MarkSurfaceUsed(gfxCachedTempSurface* aSurface)
31 if (aSurface->GetExpirationState()->IsTracked()) {
32 sExpirationTracker->MarkUsed(aSurface);
33 return;
36 if (!sExpirationTracker) {
37 sExpirationTracker = new CachedSurfaceExpirationTracker();
39 sExpirationTracker->AddObject(aSurface);
42 static void RemoveSurface(gfxCachedTempSurface* aSurface)
44 if (!sExpirationTracker)
45 return;
47 if (aSurface->GetExpirationState()->IsTracked()) {
48 sExpirationTracker->RemoveObject(aSurface);
50 if (sExpirationTracker->IsEmpty()) {
51 delete sExpirationTracker;
52 sExpirationTracker = nullptr;
56 private:
57 static CachedSurfaceExpirationTracker* sExpirationTracker;
60 CachedSurfaceExpirationTracker*
61 CachedSurfaceExpirationTracker::sExpirationTracker = nullptr;
63 gfxCachedTempSurface::~gfxCachedTempSurface()
65 CachedSurfaceExpirationTracker::RemoveSurface(this);
68 already_AddRefed<gfxContext>
69 gfxCachedTempSurface::Get(gfxASurface::gfxContentType aContentType,
70 const gfxRect& aRect,
71 gfxASurface* aSimilarTo)
73 if (mSurface) {
74 /* Verify the current buffer is valid for this purpose */
75 if (mSize.width < aRect.width || mSize.height < aRect.height
76 || mSurface->GetContentType() != aContentType) {
77 mSurface = nullptr;
78 } else {
79 NS_ASSERTION(mType == aSimilarTo->GetType(),
80 "Unexpected surface type change");
84 bool cleared = false;
85 if (!mSurface) {
86 mSize = gfxIntSize(int32_t(ceil(aRect.width)), int32_t(ceil(aRect.height)));
87 mSurface = aSimilarTo->CreateSimilarSurface(aContentType, mSize);
88 if (!mSurface)
89 return nullptr;
91 cleared = true;
92 #ifdef DEBUG
93 mType = aSimilarTo->GetType();
94 #endif
96 mSurface->SetDeviceOffset(-aRect.TopLeft());
98 nsRefPtr<gfxContext> ctx = new gfxContext(mSurface);
99 ctx->Rectangle(aRect);
100 ctx->Clip();
101 if (!cleared && aContentType != gfxASurface::CONTENT_COLOR) {
102 ctx->SetOperator(gfxContext::OPERATOR_CLEAR);
103 ctx->Paint();
104 ctx->SetOperator(gfxContext::OPERATOR_OVER);
107 CachedSurfaceExpirationTracker::MarkSurfaceUsed(this);
109 return ctx.forget();