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.

Mercurial (d1ed7de67f5a)

VCS Links

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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef GFXCOWSURFACEWRAPPER
#define GFXCOWSURFACEWRAPPER

#include "gfxImageSurface.h"
#include "nsISupportsImpl.h"
#include "nsAutoPtr.h"


/**
 * Provides an interface to implement a cross thread/process wrapper for a
 * gfxImageSurface that has copy-on-write semantics.
 *
 * Only the owner thread can write to the surface and acquire
 * read locks. Destroying a gfxReusableSurfaceWrapper releases
 * a read lock.
 *
 * OMTC Usage:
 * 1) Content creates a writable copy of this surface
 *    wrapper which will be optimized to the same wrapper if there
 *    are no readers.
 * 2) The surface is sent from content to the compositor once
 *    or potentially many times, each increasing a read lock.
 * 3) When the compositor receives the surface, it adopts the
 *    read lock.
 * 4) Once the compositor has processed the surface and uploaded
 *    the content, it then releases the read lock by dereferencing
 *    its wrapper.
 */
class gfxReusableSurfaceWrapper {
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(gfxReusableSurfaceWrapper)
public:

  /**
   * Returns a read-only pointer to the image data.
   */
  virtual const unsigned char* GetReadOnlyData() const = 0;

  /**
   * Returns the image surface format.
   */
  virtual gfxImageFormat Format() = 0;

  /**
   * Returns a writable copy of the image.
   * If necessary this will copy the wrapper. If there are no contention
   * the same wrapper will be returned. A ReadLock must be held when
   * calling this function, and calling it will give up this lock.
   */
  virtual gfxReusableSurfaceWrapper* GetWritable(gfxImageSurface** aSurface) = 0;

  /**
   * A read only lock count is recorded, any attempts to
   * call GetWritable() while this count is greater than one will
   * create a new surface/wrapper pair.
   *
   * When a surface's read count falls to zero, its memory will be
   * deallocated. It is the responsibility of the user to make sure
   * that all locks are matched with an equal number of unlocks.
   */
  virtual void ReadLock() = 0;
  virtual void ReadUnlock() = 0;

  /**
   * Types for each implementation of gfxReusableSurfaceWrapper.
   */
  enum Type {
    TYPE_SHARED_IMAGE,
    TYPE_IMAGE,

    TYPE_MAX
  };

  /**
   * Returns a unique ID for each implementation of gfxReusableSurfaceWrapper.
   */
  virtual Type GetType() = 0;

protected:
  // Protected destructor, to discourage deletion outside of Release():
  virtual ~gfxReusableSurfaceWrapper() {}

  NS_DECL_OWNINGTHREAD
};

#endif // GFXCOWSURFACEWRAPPER