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