reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame^] | 2 | Copyright 2011 Google Inc. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 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 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 17 | #ifndef GrGpu_DEFINED |
| 18 | #define GrGpu_DEFINED |
| 19 | |
| 20 | #include "GrRect.h" |
| 21 | #include "GrRefCnt.h" |
| 22 | #include "GrDrawTarget.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 23 | #include "GrTexture.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 24 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 25 | class GrVertexBufferAllocPool; |
| 26 | class GrIndexBufferAllocPool; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 27 | class GrPathRenderer; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 28 | |
| 29 | class GrGpu : public GrDrawTarget { |
| 30 | |
| 31 | public: |
| 32 | /** |
| 33 | * Possible 3D APIs that may be used by Ganesh. |
| 34 | */ |
| 35 | enum Engine { |
| 36 | kOpenGL_Shaders_Engine, |
| 37 | kOpenGL_Fixed_Engine, |
| 38 | kDirect3D9_Engine |
| 39 | }; |
| 40 | |
| 41 | /** |
| 42 | * Platform specific 3D context. |
| 43 | * For |
| 44 | * kOpenGL_Shaders_Engine use NULL |
| 45 | * kOpenGL_Fixed_Engine use NULL |
| 46 | * kDirect3D9_Engine use an IDirect3DDevice9* |
| 47 | */ |
| 48 | typedef void* Platform3DContext; |
| 49 | |
| 50 | /** |
| 51 | * Create an instance of GrGpu that matches the specified Engine backend. |
| 52 | * If the requested engine is not supported (at compile-time or run-time) |
| 53 | * this returns NULL. |
| 54 | */ |
| 55 | static GrGpu* Create(Engine, Platform3DContext context3D); |
| 56 | |
| 57 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 58 | * Used to control the level of antialiasing available for a rendertarget. |
| 59 | * Anti-alias quality levels depend on the underlying API/GPU capabilities. |
| 60 | */ |
| 61 | enum AALevels { |
| 62 | kNone_AALevel, //<! No antialiasing available. |
| 63 | kLow_AALevel, //<! Low quality antialiased rendering. Actual |
| 64 | // interpretation is platform-dependent. |
| 65 | kMed_AALevel, //<! Medium quality antialiased rendering. Actual |
| 66 | // interpretation is platform-dependent. |
| 67 | kHigh_AALevel, //<! High quality antialiased rendering. Actual |
| 68 | // interpretation is platform-dependent. |
| 69 | }; |
| 70 | |
| 71 | |
| 72 | /** |
| 73 | * Optional bitfield flags that can be passed to createTexture. |
| 74 | */ |
| 75 | enum TextureFlags { |
| 76 | kRenderTarget_TextureFlag = 0x1, //<! Creates a texture that can be |
| 77 | // rendered to by calling |
| 78 | // GrGpu::setRenderTarget() with |
| 79 | // GrTexture::asRenderTarget(). |
| 80 | kNoPathRendering_TextureFlag = 0x2, //<! If the texture is used as a |
| 81 | // rendertarget but paths will not |
| 82 | // be rendered to it. |
| 83 | kDynamicUpdate_TextureFlag = 0x4 //!< Hint that the CPU may modify |
| 84 | // this texture after creation |
| 85 | }; |
| 86 | |
| 87 | enum { |
| 88 | /** |
| 89 | * For Index8 pixel config, the colortable must be 256 entries |
| 90 | */ |
| 91 | kColorTableSize = 256 * sizeof(GrColor) |
| 92 | }; |
| 93 | /** |
| 94 | * Describes a texture to be created. |
| 95 | */ |
| 96 | struct TextureDesc { |
| 97 | uint32_t fFlags; //!< bitfield of TextureFlags |
| 98 | GrGpu::AALevels fAALevel;//!< The level of antialiasing available |
| 99 | // for a rendertarget texture. Only |
| 100 | // flags contains |
| 101 | // kRenderTarget_TextureFlag. |
| 102 | uint32_t fWidth; //!< Width of the texture |
| 103 | uint32_t fHeight; //!< Height of the texture |
| 104 | GrTexture::PixelConfig fFormat; //!< Format of source data of the |
| 105 | // texture. Not guaraunteed to be the |
| 106 | // same as internal format used by |
| 107 | // 3D API. |
| 108 | }; |
| 109 | |
| 110 | /** |
| 111 | * Gpu usage statistics. |
| 112 | */ |
| 113 | struct Stats { |
| 114 | uint32_t fVertexCnt; //<! Number of vertices drawn |
| 115 | uint32_t fIndexCnt; //<! Number of indices drawn |
| 116 | uint32_t fDrawCnt; //<! Number of draws |
| 117 | |
| 118 | uint32_t fProgChngCnt;//<! Number of program changes (N/A for fixed) |
| 119 | |
| 120 | /* |
| 121 | * Number of times the texture is set in 3D API |
| 122 | */ |
| 123 | uint32_t fTextureChngCnt; |
| 124 | /* |
| 125 | * Number of times the render target is set in 3D API |
| 126 | */ |
| 127 | uint32_t fRenderTargetChngCnt; |
| 128 | /* |
| 129 | * Number of textures created (includes textures that are rendertargets). |
| 130 | */ |
| 131 | uint32_t fTextureCreateCnt; |
| 132 | /* |
| 133 | * Number of rendertargets created. |
| 134 | */ |
| 135 | uint32_t fRenderTargetCreateCnt; |
| 136 | }; |
| 137 | |
| 138 | //////////////////////////////////////////////////////////////////////////// |
| 139 | |
| 140 | GrGpu(); |
| 141 | virtual ~GrGpu(); |
| 142 | |
| 143 | /** |
| 144 | * The GrGpu object normally assumes that no outsider is setting state |
| 145 | * within the underlying 3D API's context/device/whatever. This call informs |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 146 | * the GrGpu that the state was modified and it shouldn't make assumptions |
| 147 | * about the state. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 148 | */ |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 149 | void markContextDirty() { fContextIsDirty = true; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 150 | |
| 151 | void unimpl(const char[]); |
| 152 | |
| 153 | /** |
bsalomon@google.com | 0748f21 | 2011-02-01 22:56:16 +0000 | [diff] [blame] | 154 | * Creates a texture object. If desc width or height is not a power of |
| 155 | * two but underlying API requires a power of two texture then srcData |
| 156 | * will be embedded in a power of two texture. The extra width and height |
| 157 | * is filled as though srcData were rendered clamped into the texture. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 158 | * |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame^] | 159 | * If kRenderTarget_TextureFlag is specified the GrRenderTarget is |
| 160 | * accessible via GrTexture::asRenderTarget(). The texture will hold a ref |
| 161 | * on the render target until its releaseRenderTarget() is called or it is |
| 162 | * destroyed. |
| 163 | * |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 164 | * @param desc describes the texture to be created. |
| 165 | * @param srcData texel data to load texture. Begins with full-size |
| 166 | * palette data for paletted textures. Contains width* |
| 167 | * height texels. If NULL texture data is uninitialized. |
| 168 | * |
| 169 | * @return The texture object if successful, otherwise NULL. |
| 170 | */ |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 171 | GrTexture* createTexture(const TextureDesc& desc, |
| 172 | const void* srcData, size_t rowBytes); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 173 | /** |
| 174 | * Wraps an externally-created rendertarget in a GrRenderTarget. |
| 175 | * @param platformRenderTarget handle to the the render target in the |
| 176 | * underlying 3D API. Interpretation depends on |
| 177 | * GrGpu subclass in use. |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 178 | * @param stencilBits number of stencil bits the target has |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 179 | * @param width width of the render target |
| 180 | * @param height height of the render target |
| 181 | */ |
| 182 | virtual GrRenderTarget* createPlatformRenderTarget( |
| 183 | intptr_t platformRenderTarget, |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 184 | int stencilBits, |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 185 | int width, int height); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 186 | |
| 187 | /** |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 188 | * Reads the current target object (e.g. FBO or IDirect3DSurface9*) and |
| 189 | * viewport state from the underlying 3D API and wraps it in a |
| 190 | * GrRenderTarget. The GrRenderTarget will not attempt to delete/destroy the |
| 191 | * underlying object in its destructor and it is up to caller to guarantee |
| 192 | * that it remains valid while the GrRenderTarget is used. |
| 193 | * |
| 194 | * @return the newly created GrRenderTarget |
| 195 | */ |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 196 | GrRenderTarget* createRenderTargetFrom3DApiState(); |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 197 | |
| 198 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 199 | * Creates a vertex buffer. |
| 200 | * |
| 201 | * @param size size in bytes of the vertex buffer |
| 202 | * @param dynamic hints whether the data will be frequently changed |
| 203 | * by either GrVertexBuffer::lock or |
| 204 | * GrVertexBuffer::updateData. |
| 205 | * |
| 206 | * @return The vertex buffer if successful, otherwise NULL. |
| 207 | */ |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 208 | GrVertexBuffer* createVertexBuffer(uint32_t size, bool dynamic); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 209 | |
| 210 | /** |
| 211 | * Creates an index buffer. |
| 212 | * |
| 213 | * @param size size in bytes of the index buffer |
| 214 | * @param dynamic hints whether the data will be frequently changed |
| 215 | * by either GrIndexBuffer::lock or |
| 216 | * GrIndexBuffer::updateData. |
| 217 | * |
| 218 | * @return The index buffer if successful, otherwise NULL. |
| 219 | */ |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 220 | GrIndexBuffer* createIndexBuffer(uint32_t size, bool dynamic); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 221 | |
| 222 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 223 | * Erase the entire render target, ignoring any clips/scissors. |
| 224 | * |
| 225 | * This is issued to the GPU driver immediately. |
| 226 | */ |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 227 | void eraseColor(GrColor color); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 228 | |
| 229 | /** |
| 230 | * Are 8 bit paletted textures supported. |
| 231 | * |
| 232 | * @return true if 8bit palette textures are supported, false otherwise |
| 233 | */ |
| 234 | bool supports8BitPalette() const { return f8bitPaletteSupport; } |
| 235 | |
| 236 | /** |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 237 | * returns true if two sided stenciling is supported. If false then only |
| 238 | * the front face values of the GrStencilSettings |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 239 | * @return true if only a single stencil pass is needed. |
| 240 | */ |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 241 | bool supportsTwoSidedStencil() const |
| 242 | { return fTwoSidedStencilSupport; } |
| 243 | |
| 244 | /** |
| 245 | * returns true if stencil wrap is supported. If false then |
| 246 | * kIncWrap_StencilOp and kDecWrap_StencilOp are treated as |
| 247 | * kIncClamp_StencilOp and kDecClamp_StencilOp, respectively. |
| 248 | * @return true if stencil wrap ops are supported. |
| 249 | */ |
| 250 | bool supportsStencilWrapOps() const |
| 251 | { return fStencilWrapOpsSupport; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 252 | |
| 253 | /** |
| 254 | * Checks whether locking vertex and index buffers is supported. |
| 255 | * |
| 256 | * @return true if locking is supported. |
| 257 | */ |
| 258 | bool supportsBufferLocking() const { return fBufferLockSupport; } |
| 259 | |
| 260 | /** |
| 261 | * Gets the minimum width of a render target. If a texture/rt is created |
| 262 | * with a width less than this size the GrGpu object will clamp it to this |
| 263 | * value. |
| 264 | */ |
| 265 | int minRenderTargetWidth() const { return fMinRenderTargetWidth; } |
| 266 | |
| 267 | /** |
| 268 | * Gets the minimum width of a render target. If a texture/rt is created |
| 269 | * with a height less than this size the GrGpu object will clamp it to this |
| 270 | * value. |
| 271 | */ |
| 272 | int minRenderTargetHeight() const { return fMinRenderTargetHeight; } |
| 273 | |
| 274 | /** |
bsalomon@google.com | 0748f21 | 2011-02-01 22:56:16 +0000 | [diff] [blame] | 275 | * Returns true if NPOT textures can be created |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 276 | * |
bsalomon@google.com | 0748f21 | 2011-02-01 22:56:16 +0000 | [diff] [blame] | 277 | * @return true if NPOT textures can be created |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 278 | */ |
bsalomon@google.com | 0748f21 | 2011-02-01 22:56:16 +0000 | [diff] [blame] | 279 | bool npotTextureSupport() const { return fNPOTTextureSupport; } |
| 280 | |
| 281 | /** |
| 282 | * Returns true if NPOT textures can be repeat/mirror tiled. |
| 283 | * |
| 284 | * @return true if NPOT textures can be tiled |
| 285 | */ |
| 286 | bool npotTextureTileSupport() const { return fNPOTTextureTileSupport; } |
| 287 | |
| 288 | /** |
| 289 | * Returns true if a NPOT texture can be a rendertarget |
| 290 | * |
| 291 | * @return the true if NPOT texture/rendertarget can be created. |
| 292 | */ |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 293 | bool npotRenderTargetSupport() const { return fNPOTRenderTargetSupport; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 294 | |
reed@google.com | 02a7e6c | 2011-01-28 21:21:49 +0000 | [diff] [blame] | 295 | int maxTextureDimension() const { return fMaxTextureDimension; } |
| 296 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 297 | // GrDrawTarget overrides |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 298 | virtual void drawIndexed(GrPrimitiveType type, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 299 | int startVertex, |
| 300 | int startIndex, |
| 301 | int vertexCount, |
| 302 | int indexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 303 | |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 304 | virtual void drawNonIndexed(GrPrimitiveType type, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 305 | int startVertex, |
| 306 | int vertexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 307 | |
| 308 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 309 | * Returns an index buffer that can be used to render quads. |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 310 | * Six indices per quad: 0, 1, 2, 0, 2, 3, etc. |
| 311 | * The max number of quads can be queried using GrIndexBuffer::maxQuads(). |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 312 | * Draw with kTriangles_PrimitiveType |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 313 | * @ return the quad index buffer |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 314 | */ |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 315 | const GrIndexBuffer* getQuadIndexBuffer() const; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 316 | |
| 317 | /** |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 318 | * Returns a vertex buffer with four position-only vertices [(0,0), (1,0), |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 319 | * (1,1), (0,1)]. |
| 320 | * @ return unit square vertex buffer |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 321 | */ |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 322 | const GrVertexBuffer* getUnitSquareVertexBuffer() const; |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 323 | |
| 324 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 325 | * Ensures that the current render target is actually set in the |
| 326 | * underlying 3D API. Used when client wants to use 3D API to directly |
| 327 | * render to the RT. |
| 328 | */ |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 329 | void forceRenderTargetFlush(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 330 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 331 | /** |
| 332 | * Reads a rectangle of pixels from the current render target. |
| 333 | * @param left left edge of the rectangle to read (inclusive) |
| 334 | * @param top top edge of the rectangle to read (inclusive) |
| 335 | * @param width width of rectangle to read in pixels. |
| 336 | * @param height height of rectangle to read in pixels. |
| 337 | * @param buffer memory to read the rectangle into. |
| 338 | * |
| 339 | * @return true if the read succeeded, false if not. The read can fail |
| 340 | * because of a unsupported pixel config or because no render |
| 341 | * target is currently set. |
| 342 | */ |
| 343 | bool readPixels(int left, int top, int width, int height, |
| 344 | GrTexture::PixelConfig, void* buffer); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 345 | |
| 346 | |
| 347 | const Stats& getStats() const; |
| 348 | void resetStats(); |
| 349 | void printStats() const; |
| 350 | |
| 351 | protected: |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 352 | enum PrivateStateBits { |
| 353 | kFirstBit = (kLastPublicStateBit << 1), |
| 354 | |
| 355 | kModifyStencilClip_StateBit = kFirstBit, // allows draws to modify |
| 356 | // stencil bits used for |
| 357 | // clipping. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 358 | }; |
| 359 | |
| 360 | /** |
| 361 | * Extensions to GrDrawTarget::StateBits to implement stencil clipping |
| 362 | */ |
| 363 | struct ClipState { |
| 364 | bool fClipInStencil; |
| 365 | bool fClipIsDirty; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 366 | } fClipState; |
| 367 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 368 | // GrDrawTarget override |
| 369 | virtual void clipWillBeSet(const GrClip& newClip); |
| 370 | |
| 371 | // prepares clip flushes gpu state before a draw |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 372 | bool setupClipAndFlushState(GrPrimitiveType type); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 373 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 374 | // Functions used to map clip-respecting stencil tests into normal |
| 375 | // stencil funcs supported by GPUs. |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 376 | static GrStencilFunc ConvertStencilFunc(bool stencilInClip, |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 377 | GrStencilFunc func); |
| 378 | static void ConvertStencilFuncAndMask(GrStencilFunc func, |
| 379 | bool clipInStencil, |
| 380 | unsigned int clipBit, |
| 381 | unsigned int userBits, |
| 382 | unsigned int* ref, |
| 383 | unsigned int* mask); |
| 384 | |
| 385 | // stencil settings to clip drawing when stencil clipping is in effect |
| 386 | // and the client isn't using the stencil test. |
| 387 | static const GrStencilSettings gClipStencilSettings; |
| 388 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 389 | // defaults to false, subclass can set true to support palleted textures |
| 390 | bool f8bitPaletteSupport; |
| 391 | |
bsalomon@google.com | 0748f21 | 2011-02-01 22:56:16 +0000 | [diff] [blame] | 392 | // set by subclass |
| 393 | bool fNPOTTextureSupport; |
| 394 | bool fNPOTTextureTileSupport; |
| 395 | bool fNPOTRenderTargetSupport; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 396 | bool fTwoSidedStencilSupport; |
| 397 | bool fStencilWrapOpsSupport; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 398 | |
| 399 | // set by subclass to true if index and vertex buffers can be locked, false |
| 400 | // otherwise. |
| 401 | bool fBufferLockSupport; |
| 402 | |
| 403 | // set by subclass |
| 404 | int fMinRenderTargetWidth; |
| 405 | int fMinRenderTargetHeight; |
reed@google.com | 02a7e6c | 2011-01-28 21:21:49 +0000 | [diff] [blame] | 406 | int fMaxTextureDimension; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 407 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 408 | Stats fStats; |
| 409 | |
| 410 | const GrVertexBuffer* fCurrPoolVertexBuffer; |
| 411 | int fCurrPoolStartVertex; |
| 412 | |
| 413 | const GrIndexBuffer* fCurrPoolIndexBuffer; |
| 414 | int fCurrPoolStartIndex; |
| 415 | |
| 416 | // GrDrawTarget overrides |
| 417 | virtual bool acquireGeometryHelper(GrVertexLayout vertexLayout, |
| 418 | void** vertices, |
| 419 | void** indices); |
| 420 | virtual void releaseGeometryHelper(); |
| 421 | |
| 422 | virtual void setVertexSourceToArrayHelper(const void* vertexArray, |
| 423 | int vertexCount); |
| 424 | |
| 425 | virtual void setIndexSourceToArrayHelper(const void* indexArray, |
| 426 | int indexCount); |
| 427 | // Helpers for setting up geometry state |
| 428 | void finalizeReservedVertices(); |
| 429 | void finalizeReservedIndices(); |
| 430 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 431 | // overridden by API-specific derived class to handle re-emitting 3D API |
| 432 | // preample and dirtying state cache. |
| 433 | virtual void resetContext() = 0; |
| 434 | |
| 435 | // overridden by API-specific derived class to create objects. |
| 436 | virtual GrTexture* createTextureHelper(const TextureDesc& desc, |
| 437 | const void* srcData, |
| 438 | size_t rowBytes) = 0; |
| 439 | virtual GrRenderTarget* createPlatformRenderTargetHelper( |
| 440 | intptr_t platformRenderTarget, |
| 441 | int stencilBits, |
| 442 | int width, int height) = 0; |
| 443 | virtual GrRenderTarget* createRenderTargetFrom3DApiStateHelper() = 0; |
| 444 | virtual GrVertexBuffer* createVertexBufferHelper(uint32_t size, |
| 445 | bool dynamic) = 0; |
| 446 | virtual GrIndexBuffer* createIndexBufferHelper(uint32_t size, |
| 447 | bool dynamic) = 0; |
| 448 | |
| 449 | // overridden by API-specific derivated class to perform the erase. |
| 450 | virtual void eraseColorHelper(GrColor color) = 0; |
| 451 | |
| 452 | // overridden by API-specific derived class to perform the draw call. |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 453 | virtual void drawIndexedHelper(GrPrimitiveType type, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 454 | uint32_t startVertex, |
| 455 | uint32_t startIndex, |
| 456 | uint32_t vertexCount, |
| 457 | uint32_t indexCount) = 0; |
| 458 | |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 459 | virtual void drawNonIndexedHelper(GrPrimitiveType type, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 460 | uint32_t vertexCount, |
| 461 | uint32_t numVertices) = 0; |
| 462 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 463 | // overridden by API-specific derived class to perform flush |
| 464 | virtual void forceRenderTargetFlushHelper() = 0; |
| 465 | |
| 466 | // overridden by API-specific derived class to perform the read pixels. |
| 467 | virtual bool readPixelsHelper(int left, int top, int width, int height, |
| 468 | GrTexture::PixelConfig, void* buffer) = 0; |
| 469 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 470 | // called to program the vertex data, indexCount will be 0 if drawing non- |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 471 | // indexed geometry. The subclass may adjust the startVertex and/or |
| 472 | // startIndex since it may have already accounted for these in the setup. |
| 473 | virtual void setupGeometry(int* startVertex, |
| 474 | int* startIndex, |
| 475 | int vertexCount, |
| 476 | int indexCount) = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 477 | |
| 478 | |
| 479 | // The GrGpu typically records the clients requested state and then flushes |
| 480 | // deltas from previous state at draw time. This function does the |
| 481 | // API-specific flush of the state |
| 482 | // returns false if current state is unsupported. |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 483 | virtual bool flushGraphicsState(GrPrimitiveType type) = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 484 | |
| 485 | // Sets the scissor rect, or disables if rect is NULL. |
| 486 | virtual void flushScissor(const GrIRect* rect) = 0; |
| 487 | |
| 488 | // GrGpu subclass removes the clip from the stencil buffer |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 489 | virtual void eraseStencilClip(const GrIRect& rect) = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 490 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 491 | private: |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 492 | // readies the pools to provide vertex/index data. |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 493 | void prepareVertexPool(); |
| 494 | void prepareIndexPool(); |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 495 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 496 | GrPathRenderer* getPathRenderer(); |
| 497 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 498 | void handleDirtyContext() { |
| 499 | if (fContextIsDirty) { |
| 500 | this->resetContext(); |
| 501 | fContextIsDirty = false; |
| 502 | } |
| 503 | } |
| 504 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 505 | GrVertexBufferAllocPool* fVertexPool; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 506 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 507 | GrIndexBufferAllocPool* fIndexPool; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 508 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 509 | mutable GrIndexBuffer* fQuadIndexBuffer; // mutable so it can be |
| 510 | // created on-demand |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 511 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 512 | mutable GrVertexBuffer* fUnitSquareVertexBuffer; // mutable so it can be |
| 513 | // created on-demand |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 514 | |
| 515 | GrPathRenderer* fPathRenderer; |
| 516 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 517 | bool fContextIsDirty; |
| 518 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 519 | // when in an internal draw these indicate whether the pools are in use |
| 520 | // by one of the outer draws. If false then it is safe to reset the |
| 521 | // pool. |
| 522 | bool fVertexPoolInUse; |
| 523 | bool fIndexPoolInUse; |
| 524 | |
| 525 | // used to save and restore state when the GrGpu needs |
| 526 | // to make its geometry pools available internally |
| 527 | class AutoInternalDrawGeomRestore { |
| 528 | public: |
| 529 | AutoInternalDrawGeomRestore(GrGpu* gpu) : fAgsr(gpu) { |
| 530 | fGpu = gpu; |
| 531 | |
| 532 | fVertexPoolWasInUse = gpu->fVertexPoolInUse; |
| 533 | fIndexPoolWasInUse = gpu->fIndexPoolInUse; |
| 534 | |
| 535 | gpu->fVertexPoolInUse = fVertexPoolWasInUse || |
| 536 | (kBuffer_GeometrySrcType != |
| 537 | gpu->fGeometrySrc.fVertexSrc); |
| 538 | gpu->fIndexPoolInUse = fIndexPoolWasInUse || |
| 539 | (kBuffer_GeometrySrcType != |
| 540 | gpu->fGeometrySrc.fIndexSrc);; |
| 541 | |
| 542 | fSavedPoolVertexBuffer = gpu->fCurrPoolVertexBuffer; |
| 543 | fSavedPoolStartVertex = gpu->fCurrPoolStartVertex; |
| 544 | fSavedPoolIndexBuffer = gpu->fCurrPoolIndexBuffer; |
| 545 | fSavedPoolStartIndex = gpu->fCurrPoolStartIndex; |
| 546 | |
| 547 | fSavedReservedGeometry = gpu->fReservedGeometry; |
| 548 | gpu->fReservedGeometry.fLocked = false; |
| 549 | } |
| 550 | ~AutoInternalDrawGeomRestore() { |
| 551 | fGpu->fCurrPoolVertexBuffer = fSavedPoolVertexBuffer; |
| 552 | fGpu->fCurrPoolStartVertex = fSavedPoolStartVertex; |
| 553 | fGpu->fCurrPoolIndexBuffer = fSavedPoolIndexBuffer; |
| 554 | fGpu->fCurrPoolStartIndex = fSavedPoolStartIndex; |
| 555 | fGpu->fVertexPoolInUse = fVertexPoolWasInUse; |
| 556 | fGpu->fIndexPoolInUse = fIndexPoolWasInUse; |
| 557 | fGpu->fReservedGeometry = fSavedReservedGeometry; |
| 558 | } |
| 559 | private: |
| 560 | AutoGeometrySrcRestore fAgsr; |
| 561 | GrGpu* fGpu; |
| 562 | const GrVertexBuffer* fSavedPoolVertexBuffer; |
| 563 | int fSavedPoolStartVertex; |
| 564 | const GrIndexBuffer* fSavedPoolIndexBuffer; |
| 565 | int fSavedPoolStartIndex; |
| 566 | bool fVertexPoolWasInUse; |
| 567 | bool fIndexPoolWasInUse; |
| 568 | ReservedGeometry fSavedReservedGeometry; |
| 569 | }; |
| 570 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 571 | typedef GrDrawTarget INHERITED; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 572 | }; |
| 573 | |
| 574 | #endif |