From 3fadf04e1ce04fd77d656c641c20d954bd334f28 Mon Sep 17 00:00:00 2001 From: Jeff Muizelaar Date: Fri, 13 Jan 2012 09:47:58 -0500 Subject: [PATCH] Bug 717921. Add UserData to DrawTarget. r=bas * * * Bug 717921. Fix bustage about moz_xrealloc. --HG-- extra : rebase_source : 62d38e561d1e64c725587b1de4d1ae609b828557 --- gfx/2d/2D.h | 9 +++- gfx/2d/Makefile.in | 1 + gfx/2d/UserData.h | 108 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 gfx/2d/UserData.h diff --git a/gfx/2d/2D.h b/gfx/2d/2D.h index 0302081c0eb..c1b12a0a17a 100644 --- a/gfx/2d/2D.h +++ b/gfx/2d/2D.h @@ -41,7 +41,7 @@ #include "Point.h" #include "Rect.h" #include "Matrix.h" - +#include "UserData.h" // This RefPtr class isn't ideal for usage in Azure, as it doesn't allow T** // outparams using the &-operator. But it will have to do as there's no easy // solution. @@ -744,7 +744,14 @@ public: */ virtual void *GetNativeSurface(NativeSurfaceType aType) { return NULL; } + void AddUserData(UserDataKey *key, void *userData, void (*destroy)(void*)) { + mUserData.Add(key, userData, destroy); + } + void *GetUserData(UserDataKey *key) { + return mUserData.Get(key); + } protected: + UserData mUserData; Matrix mTransform; bool mTransformDirty : 1; diff --git a/gfx/2d/Makefile.in b/gfx/2d/Makefile.in index 8002de1cada..eb450b1550e 100644 --- a/gfx/2d/Makefile.in +++ b/gfx/2d/Makefile.in @@ -62,6 +62,7 @@ EXPORTS_mozilla/gfx = \ Matrix.h \ Rect.h \ Types.h \ + UserData.h \ $(NULL) CPPSRCS = \ diff --git a/gfx/2d/UserData.h b/gfx/2d/UserData.h new file mode 100644 index 00000000000..fb2e56f9818 --- /dev/null +++ b/gfx/2d/UserData.h @@ -0,0 +1,108 @@ +/* -*- 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 Mozilla Corporation code. + * + * The Initial Developer of the Original Code is Mozilla Foundation. + * Portions created by the Initial Developer are Copyright (Sub) 2011 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Jeff Muizelaar + * + * 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 +#include "mozilla/mozalloc.h" + +namespace mozilla { +namespace gfx { + +struct UserDataKey { + int unused; +}; + +/* this class is basically a clone of the user data concept from cairo */ +class UserData +{ + typedef void (*destroyFunc)(void *data); +public: + UserData() : count(0), entries(NULL) {} + + /* Attaches untyped userData associated with key. destroy is called on destruction */ + void Add(UserDataKey *key, void *userData, destroyFunc destroy) + { + // We could keep entries in a std::vector instead of managing it by hand + // but that would propagate an stl dependency out which we'd rather not + // do (see bug 666609). Plus, the entries array is expect to stay small + // so doing a realloc everytime we add a new entry shouldn't be too costly + entries = static_cast(moz_xrealloc(entries, sizeof(Entry)*(count+1))); + + entries[count].key = key; + entries[count].userData = userData; + entries[count].destroy = destroy; + + count++; + } + + //XXX: we probably want to add a way to remove Keys + + /* Retrives the userData for the associated key */ + void *Get(UserDataKey *key) + { + for (int i=0; i