Home | History | Annotate | Download | only in include
      1 /*
      2     Copyright 2010 Google Inc.
      3 
      4     Licensed under the Apache License, Version 2.0 (the "License");
      5     you may not use this file except in compliance with the License.
      6     You may obtain a copy of the License at
      7 
      8          http://www.apache.org/licenses/LICENSE-2.0
      9 
     10     Unless required by applicable law or agreed to in writing, software
     11     distributed under the License is distributed on an "AS IS" BASIS,
     12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13     See the License for the specific language governing permissions and
     14     limitations under the License.
     15  */
     16 
     17 
     18 #ifndef GrTypes_DEFINED
     19 #define GrTypes_DEFINED
     20 
     21 #include "SkTypes.h"
     22 #include "GrConfig.h"
     23 
     24 ////////////////////////////////////////////////////////////////////////////////
     25 
     26 /**
     27  * Defines overloaded bitwise operators to make it easier to use an enum as a
     28  * bitfield.
     29  */
     30 #define GR_MAKE_BITFIELD_OPS(X) \
     31     static inline X operator | (X a, X b) { \
     32         return (X) (+a | +b); \
     33     } \
     34     \
     35     static inline X operator & (X a, X b) { \
     36         return (X) (+a & +b); \
     37     } \
     38     template <typename T> \
     39     static inline X operator & (T a, X b) { \
     40         return (X) (+a & +b); \
     41     } \
     42     template <typename T> \
     43     static inline X operator & (X a, T b) { \
     44         return (X) (+a & +b); \
     45     } \
     46 
     47 ////////////////////////////////////////////////////////////////////////////////
     48 
     49 
     50 /**
     51  *  Macro to round n up to the next multiple of 4, or return it unchanged if
     52  *  n is already a multiple of 4
     53  */
     54 #define GrALIGN4(n)     SkAlign4(n)
     55 #define GrIsALIGN4(n)   (((n) & 3) == 0)
     56 
     57 template <typename T> const T& GrMin(const T& a, const T& b) {
     58 	return (a < b) ? a : b;
     59 }
     60 
     61 template <typename T> const T& GrMax(const T& a, const T& b) {
     62 	return (b < a) ? a : b;
     63 }
     64 
     65 // compile time versions of min/max
     66 #define GR_CT_MAX(a, b) (((b) < (a)) ? (a) : (b))
     67 #define GR_CT_MIN(a, b) (((b) < (a)) ? (b) : (a))
     68 
     69 /**
     70  *  divide, rounding up
     71  */
     72 static inline uint32_t GrUIDivRoundUp(uint32_t x, uint32_t y) {
     73     return (x + (y-1)) / y;
     74 }
     75 static inline size_t GrSizeDivRoundUp(size_t x, uint32_t y) {
     76     return (x + (y-1)) / y;
     77 }
     78 
     79 /**
     80  *  align up
     81  */
     82 static inline uint32_t GrUIAlignUp(uint32_t x, uint32_t alignment) {
     83     return GrUIDivRoundUp(x, alignment) * alignment;
     84 }
     85 static inline uint32_t GrSizeAlignUp(size_t x, uint32_t alignment) {
     86     return GrSizeDivRoundUp(x, alignment) * alignment;
     87 }
     88 
     89 /**
     90  * amount of pad needed to align up
     91  */
     92 static inline uint32_t GrUIAlignUpPad(uint32_t x, uint32_t alignment) {
     93     return (alignment - x % alignment) % alignment;
     94 }
     95 static inline size_t GrSizeAlignUpPad(size_t x, uint32_t alignment) {
     96     return (alignment - x % alignment) % alignment;
     97 }
     98 
     99 /**
    100  *  align down
    101  */
    102 static inline uint32_t GrUIAlignDown(uint32_t x, uint32_t alignment) {
    103     return (x / alignment) * alignment;
    104 }
    105 static inline uint32_t GrSizeAlignDown(size_t x, uint32_t alignment) {
    106     return (x / alignment) * alignment;
    107 }
    108 
    109 /**
    110  *  Count elements in an array
    111  */
    112 #define GR_ARRAY_COUNT(array)  SK_ARRAY_COUNT(array)
    113 
    114 //!< allocate a block of memory, will never return NULL
    115 extern void* GrMalloc(size_t bytes);
    116 
    117 //!< free block allocated by GrMalloc. ptr may be NULL
    118 extern void GrFree(void* ptr);
    119 
    120 static inline void Gr_bzero(void* dst, size_t size) {
    121     memset(dst, 0, size);
    122 }
    123 
    124 ///////////////////////////////////////////////////////////////////////////////
    125 
    126 /**
    127  *  Return the number of leading zeros in n
    128  */
    129 extern int Gr_clz(uint32_t n);
    130 
    131 /**
    132  *  Return true if n is a power of 2
    133  */
    134 static inline bool GrIsPow2(unsigned n) {
    135     return n && 0 == (n & (n - 1));
    136 }
    137 
    138 /**
    139  *  Return the next power of 2 >= n.
    140  */
    141 static inline uint32_t GrNextPow2(uint32_t n) {
    142     return n ? (1 << (32 - Gr_clz(n - 1))) : 1;
    143 }
    144 
    145 ///////////////////////////////////////////////////////////////////////////////
    146 
    147 /**
    148  *  16.16 fixed point type
    149  */
    150 typedef int32_t GrFixed;
    151 
    152 #if GR_DEBUG
    153 
    154 static inline int16_t GrToS16(intptr_t x) {
    155     GrAssert((int16_t)x == x);
    156     return (int16_t)x;
    157 }
    158 
    159 #else
    160 
    161 #define GrToS16(x)  x
    162 
    163 #endif
    164 
    165 
    166 ///////////////////////////////////////////////////////////////////////////////
    167 
    168 /**
    169  * Possible 3D APIs that may be used by Ganesh.
    170  */
    171 enum GrEngine {
    172     kOpenGL_Shaders_GrEngine,
    173     kOpenGL_Fixed_GrEngine,
    174     kDirect3D9_GrEngine
    175 };
    176 
    177 /**
    178  * Engine-specific 3D context handle
    179  *      Unused for GL.
    180  *      IDirect3DDevice9* for D3D9
    181  */
    182 typedef intptr_t GrPlatform3DContext;
    183 
    184 ///////////////////////////////////////////////////////////////////////////////
    185 
    186 /**
    187  * Type used to describe format of vertices in arrays
    188  * Values are defined in GrDrawTarget
    189  */
    190 typedef int GrVertexLayout;
    191 
    192 /**
    193 * Geometric primitives used for drawing.
    194 */
    195 enum GrPrimitiveType {
    196     kTriangles_PrimitiveType,
    197     kTriangleStrip_PrimitiveType,
    198     kTriangleFan_PrimitiveType,
    199     kPoints_PrimitiveType,
    200     kLines_PrimitiveType,
    201     kLineStrip_PrimitiveType
    202 };
    203 
    204 static inline bool GrIsPrimTypeLines(GrPrimitiveType type) {
    205     return kLines_PrimitiveType == type || kLineStrip_PrimitiveType == type;
    206 }
    207 
    208 static inline bool GrIsPrimTypeTris(GrPrimitiveType type) {
    209     return kTriangles_PrimitiveType == type     ||
    210            kTriangleStrip_PrimitiveType == type ||
    211            kTriangleFan_PrimitiveType == type;
    212 }
    213 
    214 /**
    215  * Coeffecients for alpha-blending.
    216  */
    217 enum GrBlendCoeff {
    218     kZero_BlendCoeff,    //<! 0
    219     kOne_BlendCoeff,     //<! 1
    220     kSC_BlendCoeff,      //<! src color
    221     kISC_BlendCoeff,     //<! one minus src color
    222     kDC_BlendCoeff,      //<! dst color
    223     kIDC_BlendCoeff,     //<! one minus dst color
    224     kSA_BlendCoeff,      //<! src alpha
    225     kISA_BlendCoeff,     //<! one minus src alpha
    226     kDA_BlendCoeff,      //<! dst alpha
    227     kIDA_BlendCoeff,     //<! one minus dst alpha
    228     kConstC_BlendCoeff,  //<! constant color
    229     kIConstC_BlendCoeff, //<! one minus constant color
    230     kConstA_BlendCoeff,  //<! constant color alpha
    231     kIConstA_BlendCoeff, //<! one minus constant color alpha
    232 
    233     kPublicBlendCoeffCount
    234 };
    235 
    236 /**
    237  *  Formats for masks, used by the font cache.
    238  *  Important that these are 0-based.
    239  */
    240 enum GrMaskFormat {
    241     kA8_GrMaskFormat,   //!< 1-byte per pixel
    242     kA565_GrMaskFormat  //!< 2-bytes per pixel
    243 };
    244 #define kCount_GrMaskFormats    2
    245 
    246 /**
    247  *  Return the number of bytes-per-pixel for the specified mask format.
    248  */
    249 static inline int GrMaskFormatBytesPerPixel(GrMaskFormat format) {
    250     GrAssert((unsigned)format <= 1);
    251     return (int)format + 1;
    252 }
    253 
    254 /**
    255  * Pixel configurations.
    256  */
    257 enum GrPixelConfig {
    258     kUnknown_GrPixelConfig,
    259     kAlpha_8_GrPixelConfig,
    260     kIndex_8_GrPixelConfig,
    261     kRGB_565_GrPixelConfig,
    262     kRGBA_4444_GrPixelConfig, //!< premultiplied
    263     kRGBA_8888_GrPixelConfig, //!< premultiplied
    264     kRGBX_8888_GrPixelConfig, //!< treat the alpha channel as opaque
    265 };
    266 
    267 static inline size_t GrBytesPerPixel(GrPixelConfig config) {
    268     switch (config) {
    269         case kAlpha_8_GrPixelConfig:
    270         case kIndex_8_GrPixelConfig:
    271             return 1;
    272         case kRGB_565_GrPixelConfig:
    273         case kRGBA_4444_GrPixelConfig:
    274             return 2;
    275         case kRGBA_8888_GrPixelConfig:
    276         case kRGBX_8888_GrPixelConfig:
    277             return 4;
    278         default:
    279             return 0;
    280     }
    281 }
    282 
    283 static inline bool GrPixelConfigIsOpaque(GrPixelConfig config) {
    284     switch (config) {
    285         case kRGB_565_GrPixelConfig:
    286         case kRGBX_8888_GrPixelConfig:
    287             return true;
    288         default:
    289             return false;
    290     }
    291 }
    292 
    293 static inline bool GrPixelConfigIsAlphaOnly(GrPixelConfig config) {
    294     switch (config) {
    295         case kAlpha_8_GrPixelConfig:
    296             return true;
    297         default:
    298             return false;
    299     }
    300 }
    301 
    302 /**
    303     * Used to control the level of antialiasing available for a rendertarget.
    304     * Anti-alias quality levels depend on the underlying API/GPU capabilities.
    305     */
    306 enum GrAALevels {
    307     kNone_GrAALevel, //<! No antialiasing available.
    308     kLow_GrAALevel,  //<! Low quality antialiased rendering. Actual
    309                      //   interpretation is platform-dependent.
    310     kMed_GrAALevel,  //<! Medium quality antialiased rendering. Actual
    311                      //   interpretation is platform-dependent.
    312     kHigh_GrAALevel, //<! High quality antialiased rendering. Actual
    313                      //   interpretation is platform-dependent.
    314 };
    315 
    316 /**
    317  * Optional bitfield flags that can be passed to createTexture.
    318  */
    319 enum GrTextureFlags {
    320     kNone_GrTextureFlags            = 0x0,
    321     /**
    322      * Creates a texture that can be rendered to as a GrRenderTarget. Use
    323      * GrTexture::asRenderTarget() to access.
    324      */
    325     kRenderTarget_GrTextureFlagBit  = 0x1,
    326     /**
    327      * By default all render targets have an associated stencil buffer that
    328      * may be required for path filling. This flag overrides stencil buffer
    329      * creation.
    330      * MAKE THIS PRIVATE?
    331      */
    332     kNoStencil_GrTextureFlagBit     = 0x2,
    333     /**
    334      * Hint that the CPU may modify this texture after creation.
    335      */
    336     kDynamicUpdate_GrTextureFlagBit = 0x4,
    337 };
    338 
    339 GR_MAKE_BITFIELD_OPS(GrTextureFlags)
    340 
    341 enum {
    342    /**
    343     *  For Index8 pixel config, the colortable must be 256 entries
    344     */
    345     kGrColorTableSize = 256 * 4 //sizeof(GrColor)
    346 };
    347 
    348 /**
    349  * Describes a texture to be created.
    350  */
    351 struct GrTextureDesc {
    352     GrTextureFlags         fFlags;  //!< bitfield of TextureFlags
    353     /**
    354      * The level of antialiasing available for a rendertarget texture. Only used
    355      * fFlags contains kRenderTarget_GrTextureFlag.
    356      */
    357     GrAALevels             fAALevel;
    358     uint32_t               fWidth;  //!< Width of the texture
    359     uint32_t               fHeight; //!< Height of the texture
    360     /**
    361      * Format of source data of the texture. Not guaraunteed to be the same as
    362      * internal format used by 3D API.
    363      */
    364     GrPixelConfig          fFormat;
    365 };
    366 
    367 /**
    368  * Set Operations used to construct clips.
    369  */
    370 enum GrSetOp {
    371     kReplace_SetOp,
    372     kIntersect_SetOp,
    373     kUnion_SetOp,
    374     kXor_SetOp,
    375     kDifference_SetOp,
    376     kReverseDifference_SetOp,
    377 };
    378 
    379 /**
    380  * Clips are composed from these objects.
    381  */
    382 enum GrClipType {
    383     kRect_ClipType,
    384     kPath_ClipType
    385 };
    386 
    387 /**
    388  * Commands used to describe a path. Each command
    389  * is accompanied by some number of points.
    390  */
    391 enum GrPathCmd {
    392     kMove_PathCmd,      //!< Starts a new subpath at
    393                         //   at the returned point
    394                         // 1 point
    395     kLine_PathCmd,      //!< Adds a line segment
    396                         // 2 points
    397     kQuadratic_PathCmd, //!< Adds a quadratic segment
    398                         // 3 points
    399     kCubic_PathCmd,     //!< Adds a cubic segment
    400                         // 4 points
    401     kClose_PathCmd,     //!< Closes the current subpath
    402                         //   by connecting a line to the
    403                         //   starting point.
    404                         // 0 points
    405     kEnd_PathCmd        //!< Indicates the end of the last subpath
    406                         //   when iterating
    407                         // 0 points.
    408 };
    409 
    410 /**
    411  * Gets the number of points associated with a path command.
    412  */
    413 static int inline NumPathCmdPoints(GrPathCmd cmd) {
    414     static const int gNumPoints[] = {
    415         1, 2, 3, 4, 0, 0
    416     };
    417     return gNumPoints[cmd];
    418 }
    419 
    420 /**
    421  * Path filling rules
    422  */
    423 enum GrPathFill {
    424     kWinding_PathFill,
    425     kEvenOdd_PathFill,
    426     kInverseWinding_PathFill,
    427     kInverseEvenOdd_PathFill,
    428     kHairLine_PathFill,
    429 
    430     kPathFillCount
    431 };
    432 
    433 static inline GrPathFill NonInvertedFill(GrPathFill fill) {
    434     static const GrPathFill gNonInvertedFills[] = {
    435         kWinding_PathFill, // kWinding_PathFill
    436         kEvenOdd_PathFill, // kEvenOdd_PathFill
    437         kWinding_PathFill, // kInverseWinding_PathFill
    438         kEvenOdd_PathFill, // kInverseEvenOdd_PathFill
    439         kHairLine_PathFill,// kHairLine_PathFill
    440     };
    441     GR_STATIC_ASSERT(0 == kWinding_PathFill);
    442     GR_STATIC_ASSERT(1 == kEvenOdd_PathFill);
    443     GR_STATIC_ASSERT(2 == kInverseWinding_PathFill);
    444     GR_STATIC_ASSERT(3 == kInverseEvenOdd_PathFill);
    445     GR_STATIC_ASSERT(4 == kHairLine_PathFill);
    446     GR_STATIC_ASSERT(5 == kPathFillCount);
    447     return gNonInvertedFills[fill];
    448 }
    449 
    450 static inline bool IsFillInverted(GrPathFill fill) {
    451     static const bool gIsFillInverted[] = {
    452         false, // kWinding_PathFill
    453         false, // kEvenOdd_PathFill
    454         true,  // kInverseWinding_PathFill
    455         true,  // kInverseEvenOdd_PathFill
    456         false, // kHairLine_PathFill
    457     };
    458     GR_STATIC_ASSERT(0 == kWinding_PathFill);
    459     GR_STATIC_ASSERT(1 == kEvenOdd_PathFill);
    460     GR_STATIC_ASSERT(2 == kInverseWinding_PathFill);
    461     GR_STATIC_ASSERT(3 == kInverseEvenOdd_PathFill);
    462     GR_STATIC_ASSERT(4 == kHairLine_PathFill);
    463     GR_STATIC_ASSERT(5 == kPathFillCount);
    464     return gIsFillInverted[fill];
    465 }
    466 
    467 /**
    468  * Hints provided about a path's convexity (or lack thereof).
    469  */
    470 enum GrConvexHint {
    471     kNone_ConvexHint,                         //<! No hint about convexity
    472                                               //   of the path
    473     kConvex_ConvexHint,                       //<! Path is one convex piece
    474     kNonOverlappingConvexPieces_ConvexHint,   //<! Multiple convex pieces,
    475                                               //   pieces are known to be
    476                                               //   disjoint
    477     kSameWindingConvexPieces_ConvexHint,      //<! Multiple convex pieces,
    478                                               //   may or may not intersect,
    479                                               //   either all wind cw or all
    480                                               //   wind ccw.
    481     kConcave_ConvexHint                       //<! Path is known to be
    482                                               //   concave
    483 };
    484 
    485 ///////////////////////////////////////////////////////////////////////////////
    486 
    487 enum GrPlatformSurfaceType {
    488     /**
    489      * Specifies that the object being created is a render target.
    490      */
    491     kRenderTarget_GrPlatformSurfaceType,
    492     /**
    493      * Specifies that the object being created is a texture.
    494      */
    495     kTexture_GrPlatformSurfaceType,
    496     /**
    497      * Specifies that the object being created is a texture and a render
    498      * target.
    499      */
    500     kTextureRenderTarget_GrPlatformSurfaceType,
    501 };
    502 
    503 enum GrPlatformRenderTargetFlags {
    504     kNone_GrPlatformRenderTargetFlagBit             = 0x0,
    505     /**
    506      * Specifies that the object being created is multisampled.
    507      */
    508     kIsMultisampled_GrPlatformRenderTargetFlagBit   = 0x1,
    509     /**
    510      * Gives permission to Gr to perform the downsample-resolve of a
    511      * multisampled render target. If this is not set then read pixel
    512      * operations may fail. If the object is both a texture and render target
    513      * then this *must* be set. Otherwise, if the client wants do its own
    514      * resolves it must create separate GrRenderTarget and GrTexture objects
    515      * and insert appropriate flushes and resolves betweeen data hazards.
    516      * GrRenderTarget has a flagForResolve()
    517      */
    518     kGrCanResolve_GrPlatformRenderTargetFlagBit     = 0x2,
    519 };
    520 
    521 GR_MAKE_BITFIELD_OPS(GrPlatformRenderTargetFlags)
    522 
    523 // opaque type for 3D API object handles
    524 typedef intptr_t GrPlatform3DObject;
    525 
    526 /**
    527  * Description of platform surface to create. See below for GL example.
    528  */
    529 struct GrPlatformSurfaceDesc {
    530     GrPlatformSurfaceType           fSurfaceType;   // type of surface to create
    531     /**
    532      * Flags for kRenderTarget and kTextureRenderTarget surface types
    533      */
    534     GrPlatformRenderTargetFlags     fRenderTargetFlags;
    535 
    536     int                             fWidth;         // width in pixels
    537     int                             fHeight;        // height in pixels
    538     GrPixelConfig                   fConfig;        // color format
    539     /**
    540      * Number of per sample stencil buffer. Only relevant if kIsRenderTarget is
    541      * set in fFlags.
    542      */
    543     int                             fStencilBits;
    544     /**
    545      * Texture object in 3D API. Only relevant if fSurfaceType is kTexture or
    546      * kTextureRenderTarget.
    547      * GL: this is a texture object (glGenTextures)
    548      */
    549     GrPlatform3DObject              fPlatformTexture;
    550     /**
    551      * Render target object in 3D API. Only relevant if fSurfaceType is
    552      * kRenderTarget or kTextureRenderTarget
    553      * GL: this is a FBO object (glGenFramebuffers)
    554      */
    555     GrPlatform3DObject              fPlatformRenderTarget;
    556     /**
    557      * 3D API object used as destination of resolve. Only relevant if
    558      * fSurfaceType is kRenderTarget or kTextureRenderTarget and
    559      * kGrCanResolve is set in fRenderTargetFlags.
    560      * fFlags.
    561      * GL: this is a FBO object (glGenFramebuffers)
    562      */
    563     GrPlatform3DObject              fPlatformResolveDestination;
    564 
    565     void reset() { memset(this, 0, sizeof(GrPlatformSurfaceDesc)); }
    566 };
    567 
    568 /**
    569  * Example of how to wrap render-to-texture-with-MSAA GL objects with a GrPlatformSurace
    570  *
    571  * GLint colorBufferID;
    572  * glGenRenderbuffers(1, &colorID);
    573  * glBindRenderbuffer(GL_RENDERBUFFER, colorBufferID);
    574  * glRenderbufferStorageMultisample(GL_RENDERBUFFER, S, GL_RGBA, W, H);
    575  *
    576  * GLint stencilBufferID;
    577  * glGenRenderBuffers(1, &stencilBufferID);
    578  * glBindRenderbuffer(GL_RENDERBUFFER, stencilBufferID);
    579  * glRenderbufferStorageMultisample(GL_RENDERBUFFER, S, GL_STENCIL_INDEX8, W, H);
    580  *
    581  * GLint drawFBOID;
    582  * glGenFramebuffers(1, &drawFBOID);
    583  * glBindFramebuffer(GL_FRAMEBUFFER, drawFBOID);
    584  * glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorBufferID);
    585  * glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, stencilBufferID);
    586  *
    587  * GLint textureID;
    588  * glGenTextures(1, &textureID);
    589  * glBindTexture(GL_TEXTURE_2D, textureID);
    590  * glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, W, H, ...);
    591  *
    592  * GLint readFBOID;
    593  * glGenFramebuffers(1, &readFBOID);
    594  * glBindFramebuffer(GL_FRAMEBUFFER, readFBOID);
    595  * glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, textureID, 0);
    596  *
    597  * GrPlatformSurfaceDesc renderTargetTextureDesc;
    598  * renderTargetTextureDesc.fSurfaceType       = kTextureRenderTarget_GrPlatformSurfaceType;
    599  * renderTargetTextureDesc.fRenderTargetFlags = (kIsMultisampled_GrPlatformRenderTargetFlagBit | kGrCanResolve_GrPlatformRenderTargetFlagBit);
    600  * renderTargetTextureDesc.fWidth = W;
    601  * renderTargetTextureDesc.fHeight = H;
    602  * renderTargetTextureDesc.fConfig = kRGBA_8888_GrPixelConfig
    603  * renderTargetTextureDesc.fStencilBits = 8;
    604  * renderTargetTextureDesc.fPlatformTexture = textureID;
    605  * renderTargetTextureDesc.fPlatformRenderTarget = drawFBOID;
    606  * renderTargetTextureDesc.fPlatformResolveDestination = readFBOID;
    607  *
    608  * GrTexture* texture = static_cast<GrTexture*>(grContext->createPlatrformSurface(renderTargetTextureDesc));
    609  */
    610 
    611 
    612 ///////////////////////////////////////////////////////////////////////////////
    613 
    614 // this is included only to make it easy to use this debugging facility
    615 #include "GrInstanceCounter.h"
    616 
    617 #endif
    618