| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +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 | |
| 17 | #ifndef GrContext_DEFINED |
| 18 | #define GrContext_DEFINED |
| 19 | |
| 20 | #include "GrClip.h" |
| 21 | #include "GrGpu.h" |
| 22 | #include "GrTextureCache.h" |
| 23 | #include "GrPaint.h" |
| 24 | |
| 25 | class GrFontCache; |
| 26 | class GrPathIter; |
| 27 | class GrVertexBufferAllocPool; |
| 28 | class GrIndexBufferAllocPool; |
| 29 | class GrInOrderDrawBuffer; |
| 30 | class GrPathRenderer; |
| 31 | |
| 32 | class GrContext : public GrRefCnt { |
| 33 | public: |
| 34 | /** |
| 35 | * Creates a GrContext from within a 3D context. |
| 36 | */ |
| 37 | static GrContext* Create(GrGpu::Engine engine, |
| 38 | GrGpu::Platform3DContext context3D); |
| 39 | |
| 40 | /** |
| 41 | * Helper to create a opengl-shader based context |
| 42 | */ |
| 43 | static GrContext* CreateGLShaderContext(); |
| 44 | |
| 45 | virtual ~GrContext(); |
| 46 | |
| 47 | /** |
| 48 | * The GrContext normally assumes that no outsider is setting state |
| 49 | * within the underlying 3D API's context/device/whatever. This call informs |
| 50 | * the context that the state was modified and it should resend. Shouldn't |
| 51 | * be called frequently for good performance. |
| 52 | */ |
| 53 | void resetContext(); |
| 54 | |
| 55 | /////////////////////////////////////////////////////////////////////////// |
| 56 | // Textures |
| 57 | |
| 58 | /** |
| 59 | * Abandons all textures. Call this if you have lost the associated GPU |
| 60 | * context, and thus internal texture references/IDs are now invalid. |
| 61 | */ |
| 62 | void abandonAllTextures(); |
| 63 | |
| 64 | /** |
| 65 | * Search for an entry with the same Key. If found, "lock" it and return it. |
| 66 | * If not found, return null. |
| 67 | */ |
| 68 | GrTextureEntry* findAndLockTexture(GrTextureKey*, |
| 69 | const GrSamplerState&); |
| 70 | |
| 71 | |
| 72 | /** |
| 73 | * Create a new entry, based on the specified key and texture, and return |
| 74 | * its "locked" entry. |
| 75 | * |
| 76 | * Ownership of the texture is transferred to the Entry, which will unref() |
| 77 | * it when we are purged or deleted. |
| 78 | */ |
| 79 | GrTextureEntry* createAndLockTexture(GrTextureKey* key, |
| 80 | const GrSamplerState&, |
| 81 | const GrGpu::TextureDesc&, |
| 82 | void* srcData, size_t rowBytes); |
| 83 | |
| 84 | /** |
| 85 | * When done with an entry, call unlockTexture(entry) on it, which returns |
| 86 | * it to the cache, where it may be purged. |
| 87 | */ |
| 88 | void unlockTexture(GrTextureEntry* entry); |
| 89 | |
| 90 | /** |
| 91 | * Removes an texture from the cache. This prevents the texture from |
| 92 | * being found by a subsequent findAndLockTexture() until it is |
| 93 | * reattached. The entry still counts against the cache's budget and should |
| 94 | * be reattached when exclusive access is no longer needed. |
| 95 | */ |
| 96 | void detachCachedTexture(GrTextureEntry*); |
| 97 | |
| 98 | /** |
| 99 | * Reattaches a texture to the cache and unlocks it. Allows it to be found |
| 100 | * by a subsequent findAndLock or be purged (provided its lock count is |
| 101 | * now 0.) |
| 102 | */ |
| 103 | void reattachAndUnlockCachedTexture(GrTextureEntry*); |
| 104 | |
| 105 | /** |
| 106 | * Creates a texture that is outside the cache. Does not count against |
| 107 | * cache's budget. |
| 108 | */ |
| 109 | GrTexture* createUncachedTexture(const GrGpu::TextureDesc&, |
| 110 | void* srcData, |
| 111 | size_t rowBytes); |
| 112 | |
| 113 | /** |
| 114 | * Returns true if the specified use of an indexed texture is supported. |
| 115 | */ |
| 116 | bool supportsIndex8PixelConfig(const GrSamplerState&, int width, int height); |
| 117 | |
| 118 | /** |
| 119 | * Return the current texture cache limits. |
| 120 | * |
| 121 | * @param maxTextures If non-null, returns maximum number of textures that |
| 122 | * can be held in the cache. |
| 123 | * @param maxTextureBytes If non-null, returns maximum number of bytes of |
| 124 | * texture memory that can be held in the cache. |
| 125 | */ |
| 126 | void getTextureCacheLimits(int* maxTextures, size_t* maxTextureBytes) const; |
| 127 | |
| 128 | /** |
| 129 | * Specify the texture cache limits. If the current cache exceeds either |
| 130 | * of these, it will be purged (LRU) to keep the cache within these limits. |
| 131 | * |
| 132 | * @param maxTextures The maximum number of textures that can be held in |
| 133 | * the cache. |
| 134 | * @param maxTextureBytes The maximum number of bytes of texture memory |
| 135 | * that can be held in the cache. |
| 136 | */ |
| 137 | void setTextureCacheLimits(int maxTextures, size_t maxTextureBytes); |
| 138 | |
| 139 | /** |
| 140 | * Return the max width or height of a texture supported by the current gpu |
| 141 | */ |
| 142 | int getMaxTextureDimension(); |
| 143 | |
| 144 | /////////////////////////////////////////////////////////////////////////// |
| 145 | // Render targets |
| 146 | |
| 147 | /** |
| 148 | * Wraps an externally-created rendertarget in a GrRenderTarget. |
| 149 | * @param platformRenderTarget 3D API-specific render target identifier |
| 150 | * e.g. in GL platforamRenderTarget is an FBO |
| 151 | * id. |
| 152 | * @param stencilBits the number of stencil bits that the render |
| 153 | * target has. |
| 154 | * @param width width of the render target. |
| 155 | * @param height height of the render target. |
| 156 | */ |
| 157 | GrRenderTarget* createPlatformRenderTarget(intptr_t platformRenderTarget, |
| 158 | int stencilBits, |
| 159 | int width, int height); |
| 160 | |
| 161 | /** |
| 162 | * Reads the current target object (e.g. FBO or IDirect3DSurface9*) and |
| 163 | * viewport state from the underlying 3D API and wraps it in a |
| 164 | * GrRenderTarget. The GrRenderTarget will not attempt to delete/destroy the |
| 165 | * underlying object in its destructor and it is up to caller to guarantee |
| 166 | * that it remains valid while the GrRenderTarget is used. |
| 167 | * |
| 168 | * @return the newly created GrRenderTarget |
| 169 | */ |
| 170 | GrRenderTarget* createRenderTargetFrom3DApiState() { |
| 171 | return fGpu->createRenderTargetFrom3DApiState(); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Sets the render target. |
| 176 | * @param target the render target to set. (should not be NULL.) |
| 177 | */ |
| 178 | void setRenderTarget(GrRenderTarget* target); |
| 179 | |
| 180 | /** |
| 181 | * Gets the current render target. |
| 182 | * @return the currently bound render target. Should never be NULL. |
| 183 | */ |
| 184 | const GrRenderTarget* getRenderTarget() const; |
| 185 | GrRenderTarget* getRenderTarget(); |
| 186 | |
| 187 | /////////////////////////////////////////////////////////////////////////// |
| 188 | // Matrix state |
| 189 | |
| 190 | /** |
| 191 | * Gets the current transformation matrix. |
| 192 | * @return the current matrix. |
| 193 | */ |
| 194 | const GrMatrix& getMatrix() const; |
| 195 | |
| 196 | /** |
| 197 | * Sets the transformation matrix. |
| 198 | * @param m the matrix to set. |
| 199 | */ |
| 200 | void setMatrix(const GrMatrix& m); |
| 201 | |
| 202 | /** |
| 203 | * Concats the current matrix. The passed matrix is applied before the |
| 204 | * current matrix. |
| 205 | * @param m the matrix to concat. |
| 206 | */ |
| 207 | void concatMatrix(const GrMatrix& m) const; |
| 208 | |
| 209 | |
| 210 | /////////////////////////////////////////////////////////////////////////// |
| 211 | // Clip state |
| 212 | /** |
| 213 | * Gets the current clip. |
| 214 | * @return the current clip. |
| 215 | */ |
| 216 | const GrClip& getClip() const { return fGpu->getClip(); } |
| 217 | |
| 218 | /** |
| 219 | * Sets the clip. |
| 220 | * @param clip the clip to set. |
| 221 | */ |
| 222 | void setClip(const GrClip& clip); |
| 223 | |
| 224 | /** |
| 225 | * Convenience method for setting the clip to a rect. |
| 226 | * @param rect the rect to set as the new clip. |
| 227 | */ |
| 228 | void setClip(const GrIRect& rect); |
| 229 | |
| 230 | /////////////////////////////////////////////////////////////////////////// |
| 231 | // Draws |
| 232 | |
| 233 | /** |
| 234 | * Erase the entire render target, ignoring any clips |
| 235 | */ |
| 236 | void eraseColor(GrColor color); |
| 237 | |
| 238 | /** |
| 239 | * Draw everywhere (respecting the clip) with the paint. |
| 240 | */ |
| 241 | void drawPaint(const GrPaint& paint); |
| 242 | |
| 243 | /** |
| 244 | * Draw the rect using a paint. |
| 245 | * @param paint describes how to color pixels. |
| 246 | * @param strokeWidth If strokeWidth < 0, then the rect is filled, else |
| 247 | * the rect is mitered stroked based on strokeWidth. If |
| 248 | * strokeWidth == 0, then the stroke is always a single |
| 249 | * pixel thick. |
| 250 | * @param matrix Optional matrix applied to the rect. Applied before |
| 251 | * context's matrix or the paint's matrix. |
| 252 | * The rects coords are used to access the paint (through texture matrix) |
| 253 | */ |
| 254 | void drawRect(const GrPaint& paint, |
| 255 | const GrRect&, |
| 256 | GrScalar strokeWidth = -1, |
| 257 | const GrMatrix* matrix = NULL); |
| 258 | |
| 259 | /** |
| 260 | * Maps a rect of paint coordinates onto the a rect of destination |
| 261 | * coordinates. Each rect can optionally be transformed. The srcRect |
| 262 | * is stretched over the dstRect. The dstRect is transformed by the |
| 263 | * context's matrix and the srcRect is transformed by the paint's matrix. |
| 264 | * Additional optional matrices can be provided by parameters. |
| 265 | * |
| 266 | * @param paint describes how to color pixels. |
| 267 | * @param dstRect the destination rect to draw. |
| 268 | * @param srcRect rect of paint coordinates to be mapped onto dstRect |
| 269 | * @param dstMatrix Optional matrix to transform dstRect. Applied before |
| 270 | * context's matrix. |
| 271 | * @param srcMatrix Optional matrix to transform srcRect Applied before |
| 272 | * paint's matrix. |
| 273 | */ |
| 274 | void drawRectToRect(const GrPaint& paint, |
| 275 | const GrRect& dstRect, |
| 276 | const GrRect& srcRect, |
| 277 | const GrMatrix* dstMatrix = NULL, |
| 278 | const GrMatrix* srcMatrix = NULL); |
| 279 | |
| 280 | /** |
| 281 | * Tessellates and draws a path. |
| 282 | * |
| 283 | * @param paint describes how to color pixels. |
| 284 | * @param path the path to draw |
| 285 | * @param fill the path filling rule to use. |
| 286 | * @param translate optional additional translation applied to the |
| 287 | * path. |
| 288 | */ |
| 289 | void drawPath(const GrPaint& paint, |
| 290 | GrPathIter* path, |
| 291 | GrPathFill fill, |
| 292 | const GrPoint* translate = NULL); |
| 293 | /** |
| 294 | * Draws vertices with a paint. |
| 295 | * |
| 296 | * @param paint describes how to color pixels. |
| 297 | * @param primitiveType primitives type to draw. |
| 298 | * @param vertexCount number of vertices. |
| 299 | * @param positions array of vertex positions, required. |
| 300 | * @param texCoords optional array of texture coordinates used |
| 301 | * to access the paint. |
| 302 | * @param colors optional array of per-vertex colors, supercedes |
| 303 | * the paint's color field. |
| 304 | * @param indices optional array of indices. If NULL vertices |
| 305 | * are drawn non-indexed. |
| 306 | * @param indexCount if indices is non-null then this is the |
| 307 | * number of indices. |
| 308 | */ |
| 309 | void drawVertices(const GrPaint& paint, |
| 310 | GrPrimitiveType primitiveType, |
| 311 | int vertexCount, |
| 312 | const GrPoint positions[], |
| 313 | const GrPoint texs[], |
| 314 | const GrColor colors[], |
| 315 | const uint16_t indices[], |
| 316 | int indexCount); |
| 317 | |
| 318 | /** |
| 319 | * Similar to drawVertices but caller provides objects that convert to Gr |
| 320 | * types. The count of vertices is given by posSrc. |
| 321 | * |
| 322 | * @param paint describes how to color pixels. |
| 323 | * @param primitiveType primitives type to draw. |
| 324 | * @param posSrc Source of vertex positions. Must implement |
| 325 | * int count() const; |
| 326 | * void writeValue(int i, GrPoint* point) const; |
| 327 | * count returns the total number of vertices and |
| 328 | * writeValue writes a vertex position to point. |
| 329 | * @param texSrc optional, pass NULL to not use explicit tex |
| 330 | * coords. If present provides tex coords with |
| 331 | * method: |
| 332 | * void writeValue(int i, GrPoint* point) const; |
| 333 | * @param texSrc optional, pass NULL to not use per-vertex colors |
| 334 | * If present provides colors with method: |
| 335 | * void writeValue(int i, GrColor* point) const; |
| 336 | * @param indices optional, pass NULL for non-indexed drawing. If |
| 337 | * present supplies indices for indexed drawing |
| 338 | * with following methods: |
| 339 | * int count() const; |
| 340 | * void writeValue(int i, uint16_t* point) const; |
| 341 | * count returns the number of indices and |
| 342 | * writeValue supplies each index. |
| 343 | */ |
| 344 | template <typename POS_SRC, |
| 345 | typename TEX_SRC, |
| 346 | typename COL_SRC, |
| 347 | typename IDX_SRC> |
| 348 | void drawCustomVertices(const GrPaint& paint, |
| 349 | GrPrimitiveType primitiveType, |
| 350 | const POS_SRC& posSrc, |
| 351 | const TEX_SRC* texCoordSrc, |
| 352 | const COL_SRC* colorSrc, |
| 353 | const IDX_SRC* idxSrc); |
| 354 | /** |
| 355 | * To avoid the problem of having to create a typename for NULL parameters, |
| 356 | * these reduced versions of drawCustomVertices are provided. |
| 357 | */ |
| 358 | template <typename POS_SRC> |
| 359 | void drawCustomVertices(const GrPaint& paint, |
| 360 | GrPrimitiveType primitiveType, |
| 361 | const POS_SRC& posSrc); |
| 362 | template <typename POS_SRC, typename TEX_SRC> |
| 363 | void drawCustomVertices(const GrPaint& paint, |
| 364 | GrPrimitiveType primitiveType, |
| 365 | const POS_SRC& posSrc, |
| 366 | const TEX_SRC* texCoordSrc); |
| 367 | template <typename POS_SRC, typename TEX_SRC, typename COL_SRC> |
| 368 | void drawCustomVertices(const GrPaint& paint, |
| 369 | GrPrimitiveType primitiveType, |
| 370 | const POS_SRC& posSrc, |
| 371 | const TEX_SRC* texCoordSrc, |
| 372 | const COL_SRC* colorSrc); |
| 373 | |
| 374 | |
| 375 | /////////////////////////////////////////////////////////////////////////// |
| 376 | // Misc. |
| 377 | |
| 378 | /** |
| 379 | * Call to ensure all drawing to the context has been issued to the |
| 380 | * underlying 3D API. |
| 381 | * if flushRenderTarget is true then after the call the last |
| 382 | * rendertarget set will be current in the underlying 3D API, otherwise |
| 383 | * it may not be. It is useful to set if the caller plans to use the 3D |
| 384 | * context outside of Ganesh to render into the current RT. |
| 385 | */ |
| 386 | void flush(bool flushRenderTarget); |
| 387 | |
| 388 | /** |
| 389 | * Return true on success, i.e. if we could copy the specified range of |
| 390 | * pixels from the current render-target into the buffer, converting into |
| 391 | * the specified pixel-config. |
| 392 | */ |
| 393 | bool readPixels(int left, int top, int width, int height, |
| 394 | GrTexture::PixelConfig, void* buffer); |
| 395 | |
| 396 | /** |
| 397 | * Copy the src pixels [buffer, stride, pixelconfig] into the current |
| 398 | * render-target at the specified rectangle. |
| 399 | */ |
| 400 | void writePixels(int left, int top, int width, int height, |
| 401 | GrTexture::PixelConfig, const void* buffer, size_t stride); |
| 402 | |
| 403 | |
| 404 | /////////////////////////////////////////////////////////////////////////// |
| 405 | // Statistics |
| 406 | |
| 407 | void resetStats(); |
| 408 | |
| 409 | const GrGpu::Stats& getStats() const; |
| 410 | |
| 411 | void printStats() const; |
| 412 | |
| 413 | /////////////////////////////////////////////////////////////////////////// |
| 414 | // Helpers |
| 415 | |
| 416 | class AutoRenderTarget : ::GrNoncopyable { |
| 417 | public: |
| 418 | AutoRenderTarget(GrContext* context, GrRenderTarget* target) { |
| 419 | fContext = NULL; |
| 420 | fPrevTarget = context->getRenderTarget(); |
| 421 | if (fPrevTarget != target) { |
| 422 | context->setRenderTarget(target); |
| 423 | fContext = context; |
| 424 | } |
| 425 | } |
| 426 | ~AutoRenderTarget() { |
| 427 | if (fContext) { |
| 428 | fContext->setRenderTarget(fPrevTarget); |
| 429 | } |
| 430 | } |
| 431 | private: |
| 432 | GrContext* fContext; |
| 433 | GrRenderTarget* fPrevTarget; |
| 434 | }; |
| 435 | |
| 436 | |
| 437 | /////////////////////////////////////////////////////////////////////////// |
| 438 | // Functions intended for internal use only. |
| 439 | GrGpu* getGpu() { return fGpu; } |
| 440 | GrFontCache* getFontCache() { return fFontCache; } |
| 441 | GrDrawTarget* getTextTarget(const GrPaint& paint); |
| 442 | void flushText(); |
| 443 | const GrIndexBuffer* getQuadIndexBuffer() const; |
| 444 | |
| 445 | private: |
| 446 | // used to keep track of when we need to flush the draw buffer |
| 447 | enum DrawCategory { |
| 448 | kBuffered_DrawCategory, // last draw was inserted in draw buffer |
| 449 | kUnbuffered_DrawCategory, // last draw was not inserted in the draw buffer |
| 450 | kText_DrawCategory // text context was last to draw |
| 451 | }; |
| 452 | DrawCategory fLastDrawCategory; |
| 453 | |
| 454 | GrGpu* fGpu; |
| 455 | GrTextureCache* fTextureCache; |
| 456 | GrFontCache* fFontCache; |
| 457 | GrPathRenderer* fPathRenderer; |
| 458 | |
| 459 | GrVertexBufferAllocPool* fDrawBufferVBAllocPool; |
| 460 | GrIndexBufferAllocPool* fDrawBufferIBAllocPool; |
| 461 | GrInOrderDrawBuffer* fDrawBuffer; |
| 462 | |
| 463 | GrContext(GrGpu* gpu); |
| 464 | void flushDrawBuffer(); |
| 465 | |
| 466 | static void SetPaint(const GrPaint& paint, GrDrawTarget* target); |
| 467 | |
| 468 | bool finalizeTextureKey(GrTextureKey*, const GrSamplerState&) const; |
| 469 | |
| 470 | GrDrawTarget* prepareToDraw(const GrPaint& paint, DrawCategory drawType); |
| 471 | |
| 472 | void drawClipIntoStencil(); |
| 473 | }; |
| 474 | |
| 475 | /** |
| 476 | * Save/restore the view-matrix in the context. |
| 477 | */ |
| 478 | class GrAutoMatrix : GrNoncopyable { |
| 479 | public: |
| 480 | GrAutoMatrix(GrContext* ctx) : fContext(ctx) { |
| 481 | fMatrix = ctx->getMatrix(); |
| 482 | } |
| 483 | GrAutoMatrix(GrContext* ctx, const GrMatrix& matrix) : fContext(ctx) { |
| 484 | fMatrix = ctx->getMatrix(); |
| 485 | ctx->setMatrix(matrix); |
| 486 | } |
| 487 | ~GrAutoMatrix() { |
| 488 | fContext->setMatrix(fMatrix); |
| 489 | } |
| 490 | |
| 491 | private: |
| 492 | GrContext* fContext; |
| 493 | GrMatrix fMatrix; |
| 494 | }; |
| 495 | |
| 496 | #endif |
| 497 | |
| 498 | #include "GrContext_impl.h" |