1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/2d/SourceSurfaceCG.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,192 @@ 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_SOURCESURFACECG_H 1.10 +#define _MOZILLA_GFX_SOURCESURFACECG_H 1.11 + 1.12 +#include <ApplicationServices/ApplicationServices.h> 1.13 + 1.14 +#include "2D.h" 1.15 + 1.16 +class MacIOSurface; 1.17 + 1.18 +namespace mozilla { 1.19 +namespace gfx { 1.20 + 1.21 +CGImageRef 1.22 +CreateCGImage(void *aInfo, 1.23 + const void *aData, 1.24 + const IntSize &aSize, 1.25 + int32_t aStride, 1.26 + SurfaceFormat aFormat); 1.27 + 1.28 +class DrawTargetCG; 1.29 + 1.30 +class SourceSurfaceCG : public SourceSurface 1.31 +{ 1.32 +public: 1.33 + MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(SourceSurfaceCG) 1.34 + SourceSurfaceCG() {} 1.35 + SourceSurfaceCG(CGImageRef aImage) : mImage(aImage) {} 1.36 + ~SourceSurfaceCG(); 1.37 + 1.38 + virtual SurfaceType GetType() const { return SurfaceType::COREGRAPHICS_IMAGE; } 1.39 + virtual IntSize GetSize() const; 1.40 + virtual SurfaceFormat GetFormat() const; 1.41 + virtual TemporaryRef<DataSourceSurface> GetDataSurface(); 1.42 + 1.43 + CGImageRef GetImage() { return mImage; } 1.44 + 1.45 + bool InitFromData(unsigned char *aData, 1.46 + const IntSize &aSize, 1.47 + int32_t aStride, 1.48 + SurfaceFormat aFormat); 1.49 + 1.50 +private: 1.51 + CGImageRef mImage; 1.52 + 1.53 + /* It might be better to just use the bitmap info from the CGImageRef to 1.54 + * deduce the format to save space in SourceSurfaceCG, 1.55 + * for now we just store it in mFormat */ 1.56 + SurfaceFormat mFormat; 1.57 +}; 1.58 + 1.59 +class DataSourceSurfaceCG : public DataSourceSurface 1.60 +{ 1.61 +public: 1.62 + MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DataSourceSurfaceCG) 1.63 + DataSourceSurfaceCG() {} 1.64 + DataSourceSurfaceCG(CGImageRef aImage); 1.65 + ~DataSourceSurfaceCG(); 1.66 + 1.67 + virtual SurfaceType GetType() const { return SurfaceType::DATA; } 1.68 + virtual IntSize GetSize() const; 1.69 + virtual SurfaceFormat GetFormat() const { return mFormat; } 1.70 + 1.71 + CGImageRef GetImage() { return mImage; } 1.72 + 1.73 + bool InitFromData(unsigned char *aData, 1.74 + const IntSize &aSize, 1.75 + int32_t aStride, 1.76 + SurfaceFormat aFormat); 1.77 + 1.78 + virtual unsigned char *GetData(); 1.79 + 1.80 + virtual int32_t Stride() { return CGImageGetBytesPerRow(mImage); } 1.81 + 1.82 + 1.83 +private: 1.84 + CGContextRef mCg; 1.85 + CGImageRef mImage; 1.86 + SurfaceFormat mFormat; 1.87 + //XXX: we don't need to store mData we can just get it from the CGContext 1.88 + void *mData; 1.89 + /* It might be better to just use the bitmap info from the CGImageRef to 1.90 + * deduce the format to save space in SourceSurfaceCG, 1.91 + * for now we just store it in mFormat */ 1.92 +}; 1.93 + 1.94 +class SourceSurfaceCGContext : public DataSourceSurface 1.95 +{ 1.96 +public: 1.97 + MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DataSourceSurfaceCGContext) 1.98 + virtual void DrawTargetWillChange() = 0; 1.99 + virtual CGImageRef GetImage() = 0; 1.100 +}; 1.101 + 1.102 +class SourceSurfaceCGBitmapContext : public SourceSurfaceCGContext 1.103 +{ 1.104 +public: 1.105 + MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DataSourceSurfaceCGBitmapContext) 1.106 + SourceSurfaceCGBitmapContext(DrawTargetCG *); 1.107 + ~SourceSurfaceCGBitmapContext(); 1.108 + 1.109 + virtual SurfaceType GetType() const { return SurfaceType::COREGRAPHICS_CGCONTEXT; } 1.110 + virtual IntSize GetSize() const; 1.111 + virtual SurfaceFormat GetFormat() const { return mFormat; } 1.112 + virtual TemporaryRef<DataSourceSurface> GetDataSurface() 1.113 + { 1.114 + // This call to DrawTargetWillChange() is needed to make a local copy of 1.115 + // the data from mDrawTarget. If we don't do that, the data can end up 1.116 + // getting deleted before the CGImageRef it belongs to. 1.117 + // 1.118 + // Another reason we need a local copy of the data is that the data in 1.119 + // mDrawTarget could change when someone touches the original DrawTargetCG 1.120 + // object. But a SourceSurface object should be immutable. 1.121 + // 1.122 + // For more information see bug 925448. 1.123 + DrawTargetWillChange(); 1.124 + return this; 1.125 + } 1.126 + 1.127 + CGImageRef GetImage() { EnsureImage(); return mImage; } 1.128 + 1.129 + virtual unsigned char *GetData() { return static_cast<unsigned char*>(mData); } 1.130 + 1.131 + virtual int32_t Stride() { return mStride; } 1.132 + 1.133 +private: 1.134 + //XXX: do the other backends friend their DrawTarget? 1.135 + friend class DrawTargetCG; 1.136 + virtual void DrawTargetWillChange(); 1.137 + void EnsureImage() const; 1.138 + 1.139 + // We hold a weak reference to these two objects. 1.140 + // The cycle is broken by DrawTargetWillChange 1.141 + DrawTargetCG *mDrawTarget; 1.142 + CGContextRef mCg; 1.143 + SurfaceFormat mFormat; 1.144 + 1.145 + mutable CGImageRef mImage; 1.146 + 1.147 + // mData can be owned by three different things: 1.148 + // mImage, mCg or SourceSurfaceCGBitmapContext 1.149 + void *mData; 1.150 + 1.151 + // The image buffer, if the buffer is owned by this class. 1.152 + AlignedArray<uint8_t> mDataHolder; 1.153 + 1.154 + int32_t mStride; 1.155 + IntSize mSize; 1.156 +}; 1.157 + 1.158 +class SourceSurfaceCGIOSurfaceContext : public SourceSurfaceCGContext 1.159 +{ 1.160 +public: 1.161 + MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DataSourceSurfaceCGIOSurfaceContext) 1.162 + SourceSurfaceCGIOSurfaceContext(DrawTargetCG *); 1.163 + ~SourceSurfaceCGIOSurfaceContext(); 1.164 + 1.165 + virtual SurfaceType GetType() const { return SurfaceType::COREGRAPHICS_CGCONTEXT; } 1.166 + virtual IntSize GetSize() const; 1.167 + virtual SurfaceFormat GetFormat() const { return mFormat; } 1.168 + 1.169 + CGImageRef GetImage() { EnsureImage(); return mImage; } 1.170 + 1.171 + virtual unsigned char *GetData(); 1.172 + 1.173 + virtual int32_t Stride() { return mStride; } 1.174 + 1.175 +private: 1.176 + //XXX: do the other backends friend their DrawTarget? 1.177 + friend class DrawTargetCG; 1.178 + virtual void DrawTargetWillChange(); 1.179 + void EnsureImage() const; 1.180 + 1.181 + SurfaceFormat mFormat; 1.182 + mutable CGImageRef mImage; 1.183 + MacIOSurface* mIOSurface; 1.184 + 1.185 + void *mData; 1.186 + int32_t mStride; 1.187 + 1.188 + IntSize mSize; 1.189 +}; 1.190 + 1.191 + 1.192 +} 1.193 +} 1.194 + 1.195 +#endif // _MOZILLA_GFX_SOURCESURFACECG_H