gfx/2d/BorrowedContext.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/2d/BorrowedContext.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,132 @@
     1.4 +/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef _MOZILLA_GFX_BORROWED_CONTEXT_H
    1.10 +#define _MOZILLA_GFX_BORROWED_CONTEXT_H
    1.11 +
    1.12 +#include "2D.h"
    1.13 +
    1.14 +struct _cairo;
    1.15 +typedef struct _cairo cairo_t;
    1.16 +
    1.17 +namespace mozilla {
    1.18 +
    1.19 +namespace gfx {
    1.20 +
    1.21 +/* This is a helper class that let's you borrow a cairo_t from a
    1.22 + * DrawTargetCairo. This is used for drawing themed widgets.
    1.23 + *
    1.24 + * Callers should check the cr member after constructing the object
    1.25 + * to see if it succeeded. The DrawTarget should not be used while
    1.26 + * the context is borrowed. */
    1.27 +class BorrowedCairoContext
    1.28 +{
    1.29 +public:
    1.30 +  BorrowedCairoContext()
    1.31 +    : mCairo(nullptr)
    1.32 +    , mDT(nullptr)
    1.33 +  { }
    1.34 +
    1.35 +  BorrowedCairoContext(DrawTarget *aDT)
    1.36 +    : mDT(aDT)
    1.37 +  {
    1.38 +    mCairo = BorrowCairoContextFromDrawTarget(aDT);
    1.39 +  }
    1.40 +
    1.41 +  // We can optionally Init after construction in
    1.42 +  // case we don't know what the DT will be at construction
    1.43 +  // time.
    1.44 +  cairo_t *Init(DrawTarget *aDT)
    1.45 +  {
    1.46 +    MOZ_ASSERT(!mDT, "Can't initialize twice!");
    1.47 +    mDT = aDT;
    1.48 +    return mCairo = BorrowCairoContextFromDrawTarget(aDT);
    1.49 +  }
    1.50 +
    1.51 +  // The caller needs to call Finish if cr is non-null when
    1.52 +  // they are done with the context. This is currently explicit
    1.53 +  // instead of happening implicitly in the destructor to make
    1.54 +  // what's happening in the caller more clear. It also
    1.55 +  // let's you resume using the DrawTarget in the same scope.
    1.56 +  void Finish()
    1.57 +  {
    1.58 +    if (mCairo) {
    1.59 +      ReturnCairoContextToDrawTarget(mDT, mCairo);
    1.60 +      mCairo = nullptr;
    1.61 +    }
    1.62 +  }
    1.63 +
    1.64 +  ~BorrowedCairoContext() {
    1.65 +    MOZ_ASSERT(!mCairo);
    1.66 +  }
    1.67 +
    1.68 +  cairo_t *mCairo;
    1.69 +private:
    1.70 +  static cairo_t* BorrowCairoContextFromDrawTarget(DrawTarget *aDT);
    1.71 +  static void ReturnCairoContextToDrawTarget(DrawTarget *aDT, cairo_t *aCairo);
    1.72 +  DrawTarget *mDT;
    1.73 +};
    1.74 +
    1.75 +#ifdef XP_MACOSX
    1.76 +/* This is a helper class that let's you borrow a CGContextRef from a
    1.77 + * DrawTargetCG. This is used for drawing themed widgets.
    1.78 + *
    1.79 + * Callers should check the cg member after constructing the object
    1.80 + * to see if it succeeded. The DrawTarget should not be used while
    1.81 + * the context is borrowed. */
    1.82 +class BorrowedCGContext
    1.83 +{
    1.84 +public:
    1.85 +  BorrowedCGContext()
    1.86 +    : cg(nullptr)
    1.87 +    , mDT(nullptr)
    1.88 +  { }
    1.89 +
    1.90 +  BorrowedCGContext(DrawTarget *aDT)
    1.91 +    : mDT(aDT)
    1.92 +  {
    1.93 +    cg = BorrowCGContextFromDrawTarget(aDT);
    1.94 +  }
    1.95 +
    1.96 +  // We can optionally Init after construction in
    1.97 +  // case we don't know what the DT will be at construction
    1.98 +  // time.
    1.99 +  CGContextRef Init(DrawTarget *aDT)
   1.100 +  {
   1.101 +    MOZ_ASSERT(!mDT, "Can't initialize twice!");
   1.102 +    mDT = aDT;
   1.103 +    cg = BorrowCGContextFromDrawTarget(aDT);
   1.104 +    return cg;
   1.105 +  }
   1.106 +
   1.107 +  // The caller needs to call Finish if cg is non-null when
   1.108 +  // they are done with the context. This is currently explicit
   1.109 +  // instead of happening implicitly in the destructor to make
   1.110 +  // what's happening in the caller more clear. It also
   1.111 +  // let's you resume using the DrawTarget in the same scope.
   1.112 +  void Finish()
   1.113 +  {
   1.114 +    if (cg) {
   1.115 +      ReturnCGContextToDrawTarget(mDT, cg);
   1.116 +      cg = nullptr;
   1.117 +    }
   1.118 +  }
   1.119 +
   1.120 +  ~BorrowedCGContext() {
   1.121 +    MOZ_ASSERT(!cg);
   1.122 +  }
   1.123 +
   1.124 +  CGContextRef cg;
   1.125 +private:
   1.126 +  static CGContextRef BorrowCGContextFromDrawTarget(DrawTarget *aDT);
   1.127 +  static void ReturnCGContextToDrawTarget(DrawTarget *aDT, CGContextRef cg);
   1.128 +  DrawTarget *mDT;
   1.129 +};
   1.130 +#endif
   1.131 +
   1.132 +}
   1.133 +}
   1.134 +
   1.135 +#endif // _MOZILLA_GFX_BORROWED_CONTEXT_H

mercurial