1 : /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : * ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Mozilla Corporation code.
16 : *
17 : * The Initial Developer of the Original Code is Mozilla Foundation.
18 : * Portions created by the Initial Developer are Copyright (C) 2009
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Karl Tomlinson <karlt+@karlt.net>
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "gfxCachedTempSurface.h"
39 : #include "gfxContext.h"
40 :
41 : class CachedSurfaceExpirationTracker :
42 : public nsExpirationTracker<gfxCachedTempSurface,2> {
43 :
44 : public:
45 : // With K = 2, this means that surfaces will be released when they are not
46 : // used for 1-2 seconds.
47 : enum { TIMEOUT_MS = 1000 };
48 0 : CachedSurfaceExpirationTracker()
49 0 : : nsExpirationTracker<gfxCachedTempSurface,2>(TIMEOUT_MS) {}
50 :
51 0 : ~CachedSurfaceExpirationTracker() {
52 0 : AgeAllGenerations();
53 0 : }
54 :
55 0 : virtual void NotifyExpired(gfxCachedTempSurface* aSurface) {
56 0 : RemoveObject(aSurface);
57 0 : aSurface->Expire();
58 0 : }
59 :
60 0 : static void MarkSurfaceUsed(gfxCachedTempSurface* aSurface)
61 : {
62 0 : if (aSurface->GetExpirationState()->IsTracked()) {
63 0 : sExpirationTracker->MarkUsed(aSurface);
64 0 : return;
65 : }
66 :
67 0 : if (!sExpirationTracker) {
68 0 : sExpirationTracker = new CachedSurfaceExpirationTracker();
69 : }
70 0 : sExpirationTracker->AddObject(aSurface);
71 : }
72 :
73 0 : static void RemoveSurface(gfxCachedTempSurface* aSurface)
74 : {
75 0 : if (!sExpirationTracker)
76 0 : return;
77 :
78 0 : if (aSurface->GetExpirationState()->IsTracked()) {
79 0 : sExpirationTracker->RemoveObject(aSurface);
80 : }
81 0 : if (sExpirationTracker->IsEmpty()) {
82 0 : delete sExpirationTracker;
83 0 : sExpirationTracker = nsnull;
84 : }
85 : }
86 :
87 : private:
88 : static CachedSurfaceExpirationTracker* sExpirationTracker;
89 : };
90 :
91 : CachedSurfaceExpirationTracker*
92 : CachedSurfaceExpirationTracker::sExpirationTracker = nsnull;
93 :
94 0 : gfxCachedTempSurface::~gfxCachedTempSurface()
95 : {
96 0 : CachedSurfaceExpirationTracker::RemoveSurface(this);
97 0 : }
98 :
99 : already_AddRefed<gfxContext>
100 0 : gfxCachedTempSurface::Get(gfxASurface::gfxContentType aContentType,
101 : const gfxRect& aRect,
102 : gfxASurface* aSimilarTo)
103 : {
104 0 : if (mSurface) {
105 : /* Verify the current buffer is valid for this purpose */
106 0 : if (mSize.width < aRect.width || mSize.height < aRect.height
107 0 : || mSurface->GetContentType() != aContentType) {
108 0 : mSurface = nsnull;
109 : } else {
110 0 : NS_ASSERTION(mType == aSimilarTo->GetType(),
111 : "Unexpected surface type change");
112 : }
113 : }
114 :
115 0 : bool cleared = false;
116 0 : if (!mSurface) {
117 0 : mSize = gfxIntSize(PRInt32(ceil(aRect.width)), PRInt32(ceil(aRect.height)));
118 0 : mSurface = aSimilarTo->CreateSimilarSurface(aContentType, mSize);
119 0 : if (!mSurface)
120 0 : return nsnull;
121 :
122 0 : cleared = true;
123 : #ifdef DEBUG
124 0 : mType = aSimilarTo->GetType();
125 : #endif
126 : }
127 0 : mSurface->SetDeviceOffset(-aRect.TopLeft());
128 :
129 0 : nsRefPtr<gfxContext> ctx = new gfxContext(mSurface);
130 0 : ctx->Rectangle(aRect);
131 0 : ctx->Clip();
132 0 : if (!cleared && aContentType != gfxASurface::CONTENT_COLOR) {
133 0 : ctx->SetOperator(gfxContext::OPERATOR_CLEAR);
134 0 : ctx->Paint();
135 0 : ctx->SetOperator(gfxContext::OPERATOR_OVER);
136 : }
137 :
138 0 : CachedSurfaceExpirationTracker::MarkSurfaceUsed(this);
139 :
140 0 : return ctx.forget();
141 : }
|