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