reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 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 | |
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 | * |
| 159 | * @param desc describes the texture to be created. |
| 160 | * @param srcData texel data to load texture. Begins with full-size |
| 161 | * palette data for paletted textures. Contains width* |
| 162 | * height texels. If NULL texture data is uninitialized. |
| 163 | * |
| 164 | * @return The texture object if successful, otherwise NULL. |
| 165 | */ |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame^] | 166 | GrTexture* createTexture(const TextureDesc& desc, |
| 167 | const void* srcData, size_t rowBytes); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 168 | /** |
| 169 | * Wraps an externally-created rendertarget in a GrRenderTarget. |
| 170 | * @param platformRenderTarget handle to the the render target in the |
| 171 | * underlying 3D API. Interpretation depends on |
| 172 | * GrGpu subclass in use. |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 173 | * @param stencilBits number of stencil bits the target has |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 174 | * @param width width of the render target |
| 175 | * @param height height of the render target |
| 176 | */ |
| 177 | virtual GrRenderTarget* createPlatformRenderTarget( |
| 178 | intptr_t platformRenderTarget, |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 179 | int stencilBits, |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame^] | 180 | int width, int height); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 181 | |
| 182 | /** |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 183 | * Reads the current target object (e.g. FBO or IDirect3DSurface9*) and |
| 184 | * viewport state from the underlying 3D API and wraps it in a |
| 185 | * GrRenderTarget. The GrRenderTarget will not attempt to delete/destroy the |
| 186 | * underlying object in its destructor and it is up to caller to guarantee |
| 187 | * that it remains valid while the GrRenderTarget is used. |
| 188 | * |
| 189 | * @return the newly created GrRenderTarget |
| 190 | */ |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame^] | 191 | GrRenderTarget* createRenderTargetFrom3DApiState(); |
bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 192 | |
| 193 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 194 | * Creates a vertex buffer. |
| 195 | * |
| 196 | * @param size size in bytes of the vertex buffer |
| 197 | * @param dynamic hints whether the data will be frequently changed |
| 198 | * by either GrVertexBuffer::lock or |
| 199 | * GrVertexBuffer::updateData. |
| 200 | * |
| 201 | * @return The vertex buffer if successful, otherwise NULL. |
| 202 | */ |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame^] | 203 | GrVertexBuffer* createVertexBuffer(uint32_t size, bool dynamic); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 204 | |
| 205 | /** |
| 206 | * Creates an index buffer. |
| 207 | * |
| 208 | * @param size size in bytes of the index buffer |
| 209 | * @param dynamic hints whether the data will be frequently changed |
| 210 | * by either GrIndexBuffer::lock or |
| 211 | * GrIndexBuffer::updateData. |
| 212 | * |
| 213 | * @return The index buffer if successful, otherwise NULL. |
| 214 | */ |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame^] | 215 | GrIndexBuffer* createIndexBuffer(uint32_t size, bool dynamic); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 216 | |
| 217 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 218 | * Erase the entire render target, ignoring any clips/scissors. |
| 219 | * |
| 220 | * This is issued to the GPU driver immediately. |
| 221 | */ |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame^] | 222 | void eraseColor(GrColor color); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 223 | |
| 224 | /** |
| 225 | * Are 8 bit paletted textures supported. |
| 226 | * |
| 227 | * @return true if 8bit palette textures are supported, false otherwise |
| 228 | */ |
| 229 | bool supports8BitPalette() const { return f8bitPaletteSupport; } |
| 230 | |
| 231 | /** |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 232 | * returns true if two sided stenciling is supported. If false then only |
| 233 | * the front face values of the GrStencilSettings |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 234 | * @return true if only a single stencil pass is needed. |
| 235 | */ |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 236 | bool supportsTwoSidedStencil() const |
| 237 | { return fTwoSidedStencilSupport; } |
| 238 | |
| 239 | /** |
| 240 | * returns true if stencil wrap is supported. If false then |
| 241 | * kIncWrap_StencilOp and kDecWrap_StencilOp are treated as |
| 242 | * kIncClamp_StencilOp and kDecClamp_StencilOp, respectively. |
| 243 | * @return true if stencil wrap ops are supported. |
| 244 | */ |
| 245 | bool supportsStencilWrapOps() const |
| 246 | { return fStencilWrapOpsSupport; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 247 | |
| 248 | /** |
| 249 | * Checks whether locking vertex and index buffers is supported. |
| 250 | * |
| 251 | * @return true if locking is supported. |
| 252 | */ |
| 253 | bool supportsBufferLocking() const { return fBufferLockSupport; } |
| 254 | |
| 255 | /** |
| 256 | * Gets the minimum width of a render target. If a texture/rt is created |
| 257 | * with a width less than this size the GrGpu object will clamp it to this |
| 258 | * value. |
| 259 | */ |
| 260 | int minRenderTargetWidth() const { return fMinRenderTargetWidth; } |
| 261 | |
| 262 | /** |
| 263 | * Gets the minimum width of a render target. If a texture/rt is created |
| 264 | * with a height less than this size the GrGpu object will clamp it to this |
| 265 | * value. |
| 266 | */ |
| 267 | int minRenderTargetHeight() const { return fMinRenderTargetHeight; } |
| 268 | |
| 269 | /** |
bsalomon@google.com | 0748f21 | 2011-02-01 22:56:16 +0000 | [diff] [blame] | 270 | * Returns true if NPOT textures can be created |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 271 | * |
bsalomon@google.com | 0748f21 | 2011-02-01 22:56:16 +0000 | [diff] [blame] | 272 | * @return true if NPOT textures can be created |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 273 | */ |
bsalomon@google.com | 0748f21 | 2011-02-01 22:56:16 +0000 | [diff] [blame] | 274 | bool npotTextureSupport() const { return fNPOTTextureSupport; } |
| 275 | |
| 276 | /** |
| 277 | * Returns true if NPOT textures can be repeat/mirror tiled. |
| 278 | * |
| 279 | * @return true if NPOT textures can be tiled |
| 280 | */ |
| 281 | bool npotTextureTileSupport() const { return fNPOTTextureTileSupport; } |
| 282 | |
| 283 | /** |
| 284 | * Returns true if a NPOT texture can be a rendertarget |
| 285 | * |
| 286 | * @return the true if NPOT texture/rendertarget can be created. |
| 287 | */ |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 288 | bool npotRenderTargetSupport() const { return fNPOTRenderTargetSupport; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 289 | |
reed@google.com | 02a7e6c | 2011-01-28 21:21:49 +0000 | [diff] [blame] | 290 | int maxTextureDimension() const { return fMaxTextureDimension; } |
| 291 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 292 | // GrDrawTarget overrides |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 293 | virtual void drawIndexed(GrPrimitiveType type, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 294 | int startVertex, |
| 295 | int startIndex, |
| 296 | int vertexCount, |
| 297 | int indexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 298 | |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 299 | virtual void drawNonIndexed(GrPrimitiveType type, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 300 | int startVertex, |
| 301 | int vertexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 302 | |
| 303 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 304 | * Returns an index buffer that can be used to render quads. |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 305 | * Six indices per quad: 0, 1, 2, 0, 2, 3, etc. |
| 306 | * The max number of quads can be queried using GrIndexBuffer::maxQuads(). |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 307 | * Draw with kTriangles_PrimitiveType |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 308 | * @ return the quad index buffer |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 309 | */ |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 310 | const GrIndexBuffer* getQuadIndexBuffer() const; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 311 | |
| 312 | /** |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 313 | * 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] | 314 | * (1,1), (0,1)]. |
| 315 | * @ return unit square vertex buffer |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 316 | */ |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 317 | const GrVertexBuffer* getUnitSquareVertexBuffer() const; |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 318 | |
| 319 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 320 | * Ensures that the current render target is actually set in the |
| 321 | * underlying 3D API. Used when client wants to use 3D API to directly |
| 322 | * render to the RT. |
| 323 | */ |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame^] | 324 | void forceRenderTargetFlush(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 325 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame^] | 326 | /** |
| 327 | * Reads a rectangle of pixels from the current render target. |
| 328 | * @param left left edge of the rectangle to read (inclusive) |
| 329 | * @param top top edge of the rectangle to read (inclusive) |
| 330 | * @param width width of rectangle to read in pixels. |
| 331 | * @param height height of rectangle to read in pixels. |
| 332 | * @param buffer memory to read the rectangle into. |
| 333 | * |
| 334 | * @return true if the read succeeded, false if not. The read can fail |
| 335 | * because of a unsupported pixel config or because no render |
| 336 | * target is currently set. |
| 337 | */ |
| 338 | bool readPixels(int left, int top, int width, int height, |
| 339 | GrTexture::PixelConfig, void* buffer); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 340 | |
| 341 | |
| 342 | const Stats& getStats() const; |
| 343 | void resetStats(); |
| 344 | void printStats() const; |
| 345 | |
| 346 | protected: |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 347 | enum PrivateStateBits { |
| 348 | kFirstBit = (kLastPublicStateBit << 1), |
| 349 | |
| 350 | kModifyStencilClip_StateBit = kFirstBit, // allows draws to modify |
| 351 | // stencil bits used for |
| 352 | // clipping. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 353 | }; |
| 354 | |
| 355 | /** |
| 356 | * Extensions to GrDrawTarget::StateBits to implement stencil clipping |
| 357 | */ |
| 358 | struct ClipState { |
| 359 | bool fClipInStencil; |
| 360 | bool fClipIsDirty; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 361 | } fClipState; |
| 362 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 363 | // GrDrawTarget override |
| 364 | virtual void clipWillBeSet(const GrClip& newClip); |
| 365 | |
| 366 | // prepares clip flushes gpu state before a draw |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 367 | bool setupClipAndFlushState(GrPrimitiveType type); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 368 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 369 | // Functions used to map clip-respecting stencil tests into normal |
| 370 | // stencil funcs supported by GPUs. |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame^] | 371 | static GrStencilFunc ConvertStencilFunc(bool stencilInClip, |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 372 | GrStencilFunc func); |
| 373 | static void ConvertStencilFuncAndMask(GrStencilFunc func, |
| 374 | bool clipInStencil, |
| 375 | unsigned int clipBit, |
| 376 | unsigned int userBits, |
| 377 | unsigned int* ref, |
| 378 | unsigned int* mask); |
| 379 | |
| 380 | // stencil settings to clip drawing when stencil clipping is in effect |
| 381 | // and the client isn't using the stencil test. |
| 382 | static const GrStencilSettings gClipStencilSettings; |
| 383 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 384 | // defaults to false, subclass can set true to support palleted textures |
| 385 | bool f8bitPaletteSupport; |
| 386 | |
bsalomon@google.com | 0748f21 | 2011-02-01 22:56:16 +0000 | [diff] [blame] | 387 | // set by subclass |
| 388 | bool fNPOTTextureSupport; |
| 389 | bool fNPOTTextureTileSupport; |
| 390 | bool fNPOTRenderTargetSupport; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 391 | bool fTwoSidedStencilSupport; |
| 392 | bool fStencilWrapOpsSupport; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 393 | |
| 394 | // set by subclass to true if index and vertex buffers can be locked, false |
| 395 | // otherwise. |
| 396 | bool fBufferLockSupport; |
| 397 | |
| 398 | // set by subclass |
| 399 | int fMinRenderTargetWidth; |
| 400 | int fMinRenderTargetHeight; |
reed@google.com | 02a7e6c | 2011-01-28 21:21:49 +0000 | [diff] [blame] | 401 | int fMaxTextureDimension; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 402 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 403 | Stats fStats; |
| 404 | |
| 405 | const GrVertexBuffer* fCurrPoolVertexBuffer; |
| 406 | int fCurrPoolStartVertex; |
| 407 | |
| 408 | const GrIndexBuffer* fCurrPoolIndexBuffer; |
| 409 | int fCurrPoolStartIndex; |
| 410 | |
| 411 | // GrDrawTarget overrides |
| 412 | virtual bool acquireGeometryHelper(GrVertexLayout vertexLayout, |
| 413 | void** vertices, |
| 414 | void** indices); |
| 415 | virtual void releaseGeometryHelper(); |
| 416 | |
| 417 | virtual void setVertexSourceToArrayHelper(const void* vertexArray, |
| 418 | int vertexCount); |
| 419 | |
| 420 | virtual void setIndexSourceToArrayHelper(const void* indexArray, |
| 421 | int indexCount); |
| 422 | // Helpers for setting up geometry state |
| 423 | void finalizeReservedVertices(); |
| 424 | void finalizeReservedIndices(); |
| 425 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame^] | 426 | // overridden by API-specific derived class to handle re-emitting 3D API |
| 427 | // preample and dirtying state cache. |
| 428 | virtual void resetContext() = 0; |
| 429 | |
| 430 | // overridden by API-specific derived class to create objects. |
| 431 | virtual GrTexture* createTextureHelper(const TextureDesc& desc, |
| 432 | const void* srcData, |
| 433 | size_t rowBytes) = 0; |
| 434 | virtual GrRenderTarget* createPlatformRenderTargetHelper( |
| 435 | intptr_t platformRenderTarget, |
| 436 | int stencilBits, |
| 437 | int width, int height) = 0; |
| 438 | virtual GrRenderTarget* createRenderTargetFrom3DApiStateHelper() = 0; |
| 439 | virtual GrVertexBuffer* createVertexBufferHelper(uint32_t size, |
| 440 | bool dynamic) = 0; |
| 441 | virtual GrIndexBuffer* createIndexBufferHelper(uint32_t size, |
| 442 | bool dynamic) = 0; |
| 443 | |
| 444 | // overridden by API-specific derivated class to perform the erase. |
| 445 | virtual void eraseColorHelper(GrColor color) = 0; |
| 446 | |
| 447 | // overridden by API-specific derived class to perform the draw call. |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 448 | virtual void drawIndexedHelper(GrPrimitiveType type, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 449 | uint32_t startVertex, |
| 450 | uint32_t startIndex, |
| 451 | uint32_t vertexCount, |
| 452 | uint32_t indexCount) = 0; |
| 453 | |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 454 | virtual void drawNonIndexedHelper(GrPrimitiveType type, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 455 | uint32_t vertexCount, |
| 456 | uint32_t numVertices) = 0; |
| 457 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame^] | 458 | // overridden by API-specific derived class to perform flush |
| 459 | virtual void forceRenderTargetFlushHelper() = 0; |
| 460 | |
| 461 | // overridden by API-specific derived class to perform the read pixels. |
| 462 | virtual bool readPixelsHelper(int left, int top, int width, int height, |
| 463 | GrTexture::PixelConfig, void* buffer) = 0; |
| 464 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 465 | // 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] | 466 | // indexed geometry. The subclass may adjust the startVertex and/or |
| 467 | // startIndex since it may have already accounted for these in the setup. |
| 468 | virtual void setupGeometry(int* startVertex, |
| 469 | int* startIndex, |
| 470 | int vertexCount, |
| 471 | int indexCount) = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 472 | |
| 473 | |
| 474 | // The GrGpu typically records the clients requested state and then flushes |
| 475 | // deltas from previous state at draw time. This function does the |
| 476 | // API-specific flush of the state |
| 477 | // returns false if current state is unsupported. |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 478 | virtual bool flushGraphicsState(GrPrimitiveType type) = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 479 | |
| 480 | // Sets the scissor rect, or disables if rect is NULL. |
| 481 | virtual void flushScissor(const GrIRect* rect) = 0; |
| 482 | |
| 483 | // GrGpu subclass removes the clip from the stencil buffer |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 484 | virtual void eraseStencilClip(const GrIRect& rect) = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 485 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 486 | private: |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 487 | // readies the pools to provide vertex/index data. |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 488 | void prepareVertexPool(); |
| 489 | void prepareIndexPool(); |
bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 490 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 491 | GrPathRenderer* getPathRenderer(); |
| 492 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame^] | 493 | void handleDirtyContext() { |
| 494 | if (fContextIsDirty) { |
| 495 | this->resetContext(); |
| 496 | fContextIsDirty = false; |
| 497 | } |
| 498 | } |
| 499 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 500 | GrVertexBufferAllocPool* fVertexPool; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 501 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 502 | GrIndexBufferAllocPool* fIndexPool; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 503 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 504 | mutable GrIndexBuffer* fQuadIndexBuffer; // mutable so it can be |
| 505 | // created on-demand |
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 | mutable GrVertexBuffer* fUnitSquareVertexBuffer; // mutable so it can be |
| 508 | // created on-demand |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 509 | |
| 510 | GrPathRenderer* fPathRenderer; |
| 511 | |
bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame^] | 512 | bool fContextIsDirty; |
| 513 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 514 | // when in an internal draw these indicate whether the pools are in use |
| 515 | // by one of the outer draws. If false then it is safe to reset the |
| 516 | // pool. |
| 517 | bool fVertexPoolInUse; |
| 518 | bool fIndexPoolInUse; |
| 519 | |
| 520 | // used to save and restore state when the GrGpu needs |
| 521 | // to make its geometry pools available internally |
| 522 | class AutoInternalDrawGeomRestore { |
| 523 | public: |
| 524 | AutoInternalDrawGeomRestore(GrGpu* gpu) : fAgsr(gpu) { |
| 525 | fGpu = gpu; |
| 526 | |
| 527 | fVertexPoolWasInUse = gpu->fVertexPoolInUse; |
| 528 | fIndexPoolWasInUse = gpu->fIndexPoolInUse; |
| 529 | |
| 530 | gpu->fVertexPoolInUse = fVertexPoolWasInUse || |
| 531 | (kBuffer_GeometrySrcType != |
| 532 | gpu->fGeometrySrc.fVertexSrc); |
| 533 | gpu->fIndexPoolInUse = fIndexPoolWasInUse || |
| 534 | (kBuffer_GeometrySrcType != |
| 535 | gpu->fGeometrySrc.fIndexSrc);; |
| 536 | |
| 537 | fSavedPoolVertexBuffer = gpu->fCurrPoolVertexBuffer; |
| 538 | fSavedPoolStartVertex = gpu->fCurrPoolStartVertex; |
| 539 | fSavedPoolIndexBuffer = gpu->fCurrPoolIndexBuffer; |
| 540 | fSavedPoolStartIndex = gpu->fCurrPoolStartIndex; |
| 541 | |
| 542 | fSavedReservedGeometry = gpu->fReservedGeometry; |
| 543 | gpu->fReservedGeometry.fLocked = false; |
| 544 | } |
| 545 | ~AutoInternalDrawGeomRestore() { |
| 546 | fGpu->fCurrPoolVertexBuffer = fSavedPoolVertexBuffer; |
| 547 | fGpu->fCurrPoolStartVertex = fSavedPoolStartVertex; |
| 548 | fGpu->fCurrPoolIndexBuffer = fSavedPoolIndexBuffer; |
| 549 | fGpu->fCurrPoolStartIndex = fSavedPoolStartIndex; |
| 550 | fGpu->fVertexPoolInUse = fVertexPoolWasInUse; |
| 551 | fGpu->fIndexPoolInUse = fIndexPoolWasInUse; |
| 552 | fGpu->fReservedGeometry = fSavedReservedGeometry; |
| 553 | } |
| 554 | private: |
| 555 | AutoGeometrySrcRestore fAgsr; |
| 556 | GrGpu* fGpu; |
| 557 | const GrVertexBuffer* fSavedPoolVertexBuffer; |
| 558 | int fSavedPoolStartVertex; |
| 559 | const GrIndexBuffer* fSavedPoolIndexBuffer; |
| 560 | int fSavedPoolStartIndex; |
| 561 | bool fVertexPoolWasInUse; |
| 562 | bool fIndexPoolWasInUse; |
| 563 | ReservedGeometry fSavedReservedGeometry; |
| 564 | }; |
| 565 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 566 | typedef GrDrawTarget INHERITED; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 567 | }; |
| 568 | |
| 569 | #endif |