1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef _MOZILLA_GFX_DATASURFACEHELPERS_H 8 #define _MOZILLA_GFX_DATASURFACEHELPERS_H 9 10 #include "2D.h" 11 12 #include "mozilla/UniquePtr.h" 13 14 namespace mozilla { 15 namespace gfx { 16 17 int32_t StrideForFormatAndWidth(SurfaceFormat aFormat, int32_t aWidth); 18 19 /** 20 * Create a DataSourceSurface and init the surface with the |aData|. The stride 21 * of this source surface might be different from the input data's 22 * |aDataStride|. System will try to use the optimal one. 23 */ 24 already_AddRefed<DataSourceSurface> CreateDataSourceSurfaceFromData( 25 const IntSize& aSize, SurfaceFormat aFormat, const uint8_t* aData, 26 int32_t aDataStride); 27 28 /** 29 * Similar to CreateDataSourceSurfaceFromData(), but could setup the stride for 30 * this surface. 31 */ 32 already_AddRefed<DataSourceSurface> CreateDataSourceSurfaceWithStrideFromData( 33 const IntSize& aSize, SurfaceFormat aFormat, int32_t aStride, 34 const uint8_t* aData, int32_t aDataStride); 35 36 /** 37 * Copy the pixel data from aSrc and pack it into aDst. aSrcSize, aSrcStride 38 * and aBytesPerPixel give the size, stride and bytes per pixel for aSrc's 39 * surface. Callers are responsible for making sure that aDst is big enough to 40 * contain |aSrcSize.width * aSrcSize.height * aBytesPerPixel| bytes. 41 */ 42 void CopySurfaceDataToPackedArray(uint8_t* aSrc, uint8_t* aDst, 43 IntSize aSrcSize, int32_t aSrcStride, 44 int32_t aBytesPerPixel); 45 46 /** 47 * Convert aSurface to a packed buffer in BGRA format. 48 */ 49 UniquePtr<uint8_t[]> SurfaceToPackedBGRA(DataSourceSurface* aSurface); 50 51 /** 52 * Convert aSurface to a packed buffer in BGR format. The pixel data is 53 * returned in a buffer allocated with new uint8_t[]. The caller then has 54 * ownership of the buffer and is responsible for delete[]'ing it. 55 * 56 * This function is currently only intended for use with surfaces of format 57 * SurfaceFormat::B8G8R8X8 since the X components of the pixel data (if any) 58 * are simply dropped (no attempt is made to un-pre-multiply alpha from the 59 * color components). 60 */ 61 uint8_t* SurfaceToPackedBGR(DataSourceSurface* aSurface); 62 63 /** 64 * Clears all the bytes in a DataSourceSurface's data array to zero (so to 65 * transparent black for SurfaceFormat::B8G8R8A8, for example). 66 * Note that DataSourceSurfaces can be initialized to zero, which is 67 * more efficient than zeroing the surface after initialization. 68 */ 69 void ClearDataSourceSurface(DataSourceSurface* aSurface); 70 71 /** 72 * Multiplies aStride and aHeight and makes sure the result is limited to 73 * something sane. To keep things consistent, this should always be used 74 * wherever we allocate a buffer based on surface stride and height. 75 * 76 * @param aExtra Optional argument to specify an additional number of trailing 77 * bytes (useful for creating intermediate surfaces for filters, for 78 * example). 79 * 80 * @return The result of the multiplication if it is acceptable, or else zero. 81 */ 82 size_t BufferSizeFromStrideAndHeight(int32_t aStride, int32_t aHeight, 83 int32_t aExtraBytes = 0); 84 85 /** 86 * Multiplies aWidth, aHeight, aDepth and makes sure the result is limited to 87 * something sane. To keep things consistent, this should always be used 88 * wherever we allocate a buffer based on surface dimensions. 89 * 90 * @param aExtra Optional argument to specify an additional number of trailing 91 * bytes (useful for creating intermediate surfaces for filters, for 92 * example). 93 * 94 * @return The result of the multiplication if it is acceptable, or else zero. 95 */ 96 size_t BufferSizeFromDimensions(int32_t aWidth, int32_t aHeight, int32_t aDepth, 97 int32_t aExtraBytes = 0); 98 /** 99 * Copy aSrcRect from aSrc to aDest starting at aDestPoint. 100 * @returns false if the copy is not successful or the aSrc's size is empty. 101 */ 102 bool CopyRect(DataSourceSurface* aSrc, DataSourceSurface* aDest, 103 IntRect aSrcRect, IntPoint aDestPoint); 104 105 /** 106 * Create a non aliasing copy of aSource. This creates a new DataSourceSurface 107 * using the factory and copies the bits. 108 * 109 * @return a dss allocated by Factory that contains a copy a aSource. 110 */ 111 already_AddRefed<DataSourceSurface> CreateDataSourceSurfaceByCloning( 112 DataSourceSurface* aSource); 113 114 /** 115 * Return the byte at aPoint. 116 */ 117 uint8_t* DataAtOffset(DataSourceSurface* aSurface, 118 const DataSourceSurface::MappedSurface* aMap, 119 IntPoint aPoint); 120 121 /** 122 * Check if aPoint is contained by the surface. 123 * 124 * @returns true if and only if aPoint is inside the surface. 125 */ 126 bool SurfaceContainsPoint(SourceSurface* aSurface, const IntPoint& aPoint); 127 128 } // namespace gfx 129 } // namespace mozilla 130 131 #endif // _MOZILLA_GFX_DATASURFACEHELPERS_H 132