DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Untracked file

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is supposed to avoid excessive QuickDraw flushes.
 *
 * The Initial Developer of the Original Code is
 * Mark Mentovai <mark@moxienet.com>.
 * Portions created by the Initial Developer are Copyright (C) 2005
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

#include "nsQDFlushManager.h"

#include "nsComponentManagerUtils.h"
#include "nsIServiceManager.h"
#include "nsCRT.h"

// nsQDFlushManager

nsQDFlushManager::nsQDFlushManager()
: mPortList(nsnull)
{
}

nsQDFlushManager::~nsQDFlushManager()
{
  nsQDFlushPort* port = mPortList;
  while (port)
  {
    nsQDFlushPort* next = port->mNext;
    port->Destroy();
    NS_RELEASE(port);
    port = next;
  }
}

// CreateOrGetPort(aPort)
//
// Walks through the list of port objects and returns the one corresponding to
// aPort if it exists.  If it doesn't exist, but an unused existing port
// object can be adapted to aPort, it will be adapted and returned.  If no
// suitable port object exists, a new port object is created, added to the
// list, and returned.  Any port objects created here will be destroyed when
// the ~nsQDFlushManager destructor runs or when RemovePort is called.
//
// protected
nsQDFlushPort*
nsQDFlushManager::CreateOrGetPort(CGrafPtr aPort)
{
  AbsoluteTime now = ::UpTime();

  // First, go through the list and see if an object is already associated
  // with this port.
  nsQDFlushPort* port = mPortList;
  while (port)
  {
    if (aPort == port->mPort)
    {
      return port;
    }
    port = port->mNext;
  }

  // If no port object exists yet, try to find an object that's not in use.
  // If there is one, repurpose it.
  // Don't be frightened.  The pointer-pointer business isn't so confusing,
  // and it eases maintenance of the linked list.
  nsQDFlushPort** portPtr = &mPortList;
  while ((port = *portPtr))
  {
    if (!port->mFlushTimerRunning && port->TimeUntilFlush(now) < 0)
    {
      // If the flush timer is not running and it's past the time during which
      // a flush would be postponed, the object is no longer needed.  Future
      // flushes for port->mPort would occur immediately.  Since there's no
      // longer any state to track, the object can be reused for another port
      // This keeps the size of the list manageable.
      port->Init(aPort);
      return port;
    }
    portPtr = &port->mNext;
  }

  // portPtr points to mNext of the last port object in the list, or if none,
  // to mPortList.  That makes it easy to hook the new object up.
  *portPtr = port = new nsQDFlushPort(aPort);
  NS_IF_ADDREF(port);
  return port;
}

NS_IMPL_ISUPPORTS1(nsQDFlushManager, nsIQDFlushManager)

// nsIQDFlushManager implementation

// FlushPortBuffer(aPort, aRegion)
//
// The public entry point for object-based calls.  Calls
// QDFlushPortBuffer(aPort, aRegion) if aPort hasn't been flushed too
// recently.  If it has been, calls QDAddRegionToDirtyRegion(aPort, aRegion)
// and if no flush has been scheduled, schedules a flush for the appropriate
// time.
//
// public
NS_IMETHODIMP
nsQDFlushManager::FlushPortBuffer(CGrafPtr aPort, RgnHandle aRegion)
{
  CreateOrGetPort(aPort)->FlushPortBuffer(aRegion);
  return NS_OK;
}

// RemovePort(aPort)
//
// Walks through the list of port objects and removes the one corresponding to
// aPort, if it exists.
//
// public
NS_IMETHODIMP
nsQDFlushManager::RemovePort(CGrafPtr aPort)
{
  // Traversal is as in CreateOrGetPort.
  nsQDFlushPort** portPtr = &mPortList;
  while (nsQDFlushPort* port = *portPtr)
  {
    if (aPort == port->mPort)
    {
      nsQDFlushPort* next = port->mNext;
      port->Destroy();
      NS_RELEASE(port);

      // portPtr points to mNext of the previous object, or if none,
      // mPortList.  That makes it easy to snip the old object out by
      // setting it to the follower.
      *portPtr = next;
      return NS_OK;
    }
    portPtr = &port->mNext;
  }
  return NS_OK;
}

// nsQDFlushPort

nsQDFlushPort::nsQDFlushPort(CGrafPtr aPort)
: mNext(nsnull)
, mPort(aPort)
, mLastFlushTime((AbsoluteTime){0, 0})
, mFlushTimer(nsnull)
, mFlushTimerRunning(PR_FALSE)
{
}

nsQDFlushPort::~nsQDFlushPort()
{
  // Everything should have been taken care of by Destroy().
}

// Init(aPort)
//
// (Re)initialize object.
//
// protected
void
nsQDFlushPort::Init(CGrafPtr aPort)
{
  mPort = aPort;
}

// Destroy()
//
// Prepare object for destruction.
//
// protected
void
nsQDFlushPort::Destroy()
{
  if (mFlushTimer)
  {
    mFlushTimer->Cancel();
  }
  mFlushTimer = nsnull;
  mFlushTimerRunning = PR_FALSE;
  mNext = nsnull;
}

// FlushPortBuffer(aRegion)
//
// Flushes, dirties, and schedules, as appropriate.  Public access is from
// nsQDFlushManager::FlushPortBuffer(CGrafPtr aPort, RgnHandle aRegion).
//
// protected
void
nsQDFlushPort::FlushPortBuffer(RgnHandle aRegion)
{
  AbsoluteTime now = ::UpTime();
  PRInt64 timeUntilFlush = TimeUntilFlush(now);

  if (!mFlushTimerRunning && timeUntilFlush < 0)
  {
    // If past the time for the next acceptable flush, flush now.
    ::QDFlushPortBuffer(mPort, aRegion);
    mLastFlushTime = now;
  }
  else
  {
    // If it's not time for the next flush yet, or if the timer is running
    // indicating that an update is pending, just mark the dirty region.
    ::QDAddRegionToDirtyRegion(mPort, aRegion);

    if (!mFlushTimerRunning)
    {
      // No flush scheduled?  No problem.
      if (!mFlushTimer)
      {
        // No timer object?  No problem.
        nsresult err;
        mFlushTimer = do_CreateInstance("@mozilla.org/timer;1", &err);
        NS_ASSERTION(NS_SUCCEEDED(err), "Could not instantiate flush timer.");
      }
      if (mFlushTimer)
      {
        // Start the clock, with the timer firing at the already-calculated
        // time until the next flush.  Nanoseconds (1E-9) were used above,
        // but nsITimer is big on milliseconds (1E-3), so divide by 1E6.
        // Any time that was consumed between the ::UpTime call and now
        // will be lost.  That's not so bad in the usual case, it's a tiny
        // bit less not so bad if a timer object didn't exist yet and was
        // created.  It's better to update slightly less frequently than
        // the target than slightly more frequently.
        mFlushTimer->InitWithCallback(this, (PRUint32)(timeUntilFlush/1E6),
                                      nsITimer::TYPE_ONE_SHOT);
        mFlushTimerRunning = PR_TRUE;
      }
    }
  }
}

// protected
PRInt64
nsQDFlushPort::TimeUntilFlush(AbsoluteTime aNow)
{
  Nanoseconds elapsed = ::AbsoluteDeltaToNanoseconds(aNow, mLastFlushTime);

  // nano = 1E-9 and the desired refresh rate is in Hz, so 1E9/kRefreshRateHz
  // gives the interval between updates in nanoseconds.
  return S64Subtract(U64SetU(1E9/kRefreshRateHz),
                     UnsignedWideToUInt64(elapsed));
}

// nsITimer implementation

NS_IMPL_ISUPPORTS1(nsQDFlushPort, nsITimerCallback)

// Notify(aTimer)
//
// Timer callback.  Flush the dirty port buffer to the screen.
NS_IMETHODIMP
nsQDFlushPort::Notify(nsITimer* aTimer)
{
  NS_ASSERTION(aTimer == mFlushTimer, "Callback called by wrong timer");

  // Flush the dirty region.
  ::QDFlushPortBuffer(mPort, NULL);

  mLastFlushTime = ::UpTime();
  mFlushTimerRunning = PR_FALSE;

  // This shouldn't be necessary, nsITimer.idl
  // aTimer->Cancel();

  return NS_OK;
}