GrContext.h revision e137db8569e7a10d809980609688b3acfaec612a
1/* 2 * Copyright 2010 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8#ifndef GrContext_DEFINED 9#define GrContext_DEFINED 10 11#include "GrClipData.h" 12#include "GrColor.h" 13#include "GrPaint.h" 14#include "GrPathRendererChain.h" 15#include "GrRenderTarget.h" 16#include "GrTexture.h" 17#include "SkMatrix.h" 18#include "SkPathEffect.h" 19#include "SkTypes.h" 20 21class GrAARectRenderer; 22class GrDrawTarget; 23class GrFontCache; 24class GrFragmentProcessor; 25class GrGpu; 26class GrGpuTraceMarker; 27class GrIndexBuffer; 28class GrIndexBufferAllocPool; 29class GrInOrderDrawBuffer; 30class GrLayerCache; 31class GrOvalRenderer; 32class GrPath; 33class GrPathRenderer; 34class GrPipelineBuilder; 35class GrResourceEntry; 36class GrResourceCache2; 37class GrTestTarget; 38class GrTextContext; 39class GrTextureParams; 40class GrVertexBuffer; 41class GrVertexBufferAllocPool; 42class GrStrokeInfo; 43class GrSoftwarePathRenderer; 44class SkStrokeRec; 45 46class SK_API GrContext : public SkRefCnt { 47public: 48 SK_DECLARE_INST_COUNT(GrContext) 49 50 struct Options { 51 Options() : fDrawPathToCompressedTexture(false) { } 52 53 // EXPERIMENTAL 54 // May be removed in the future, or may become standard depending 55 // on the outcomes of a variety of internal tests. 56 bool fDrawPathToCompressedTexture; 57 }; 58 59 /** 60 * Creates a GrContext for a backend context. 61 */ 62 static GrContext* Create(GrBackend, GrBackendContext, const Options* opts = NULL); 63 64 /** 65 * Only defined in test apps. 66 */ 67 static GrContext* CreateMockContext(); 68 69 virtual ~GrContext(); 70 71 /** 72 * The GrContext normally assumes that no outsider is setting state 73 * within the underlying 3D API's context/device/whatever. This call informs 74 * the context that the state was modified and it should resend. Shouldn't 75 * be called frequently for good performance. 76 * The flag bits, state, is dpendent on which backend is used by the 77 * context, either GL or D3D (possible in future). 78 */ 79 void resetContext(uint32_t state = kAll_GrBackendState); 80 81 /** 82 * Callback function to allow classes to cleanup on GrContext destruction. 83 * The 'info' field is filled in with the 'info' passed to addCleanUp. 84 */ 85 typedef void (*PFCleanUpFunc)(const GrContext* context, void* info); 86 87 /** 88 * Add a function to be called from within GrContext's destructor. 89 * This gives classes a chance to free resources held on a per context basis. 90 * The 'info' parameter will be stored and passed to the callback function. 91 */ 92 void addCleanUp(PFCleanUpFunc cleanUp, void* info) { 93 CleanUpData* entry = fCleanUpData.push(); 94 95 entry->fFunc = cleanUp; 96 entry->fInfo = info; 97 } 98 99 /** 100 * Abandons all GPU resources and assumes the underlying backend 3D API 101 * context is not longer usable. Call this if you have lost the associated 102 * GPU context, and thus internal texture, buffer, etc. references/IDs are 103 * now invalid. Should be called even when GrContext is no longer going to 104 * be used for two reasons: 105 * 1) ~GrContext will not try to free the objects in the 3D API. 106 * 2) Any GrGpuResources created by this GrContext that outlive 107 * will be marked as invalid (GrGpuResource::wasDestroyed()) and 108 * when they're destroyed no 3D API calls will be made. 109 * Content drawn since the last GrContext::flush() may be lost. After this 110 * function is called the only valid action on the GrContext or 111 * GrGpuResources it created is to destroy them. 112 */ 113 void abandonContext(); 114 void contextDestroyed() { this->abandonContext(); } // legacy alias 115 116 /////////////////////////////////////////////////////////////////////////// 117 // Resource Cache 118 119 /** 120 * Return the current GPU resource cache limits. 121 * 122 * @param maxResources If non-null, returns maximum number of resources that 123 * can be held in the cache. 124 * @param maxResourceBytes If non-null, returns maximum number of bytes of 125 * video memory that can be held in the cache. 126 */ 127 void getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const; 128 129 /** 130 * Gets the current GPU resource cache usage. 131 * 132 * @param resourceCount If non-null, returns the number of resources that are held in the 133 * cache. 134 * @param maxResourceBytes If non-null, returns the total number of bytes of video memory held 135 * in the cache. 136 */ 137 void getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const; 138 139 /** 140 * Specify the GPU resource cache limits. If the current cache exceeds either 141 * of these, it will be purged (LRU) to keep the cache within these limits. 142 * 143 * @param maxResources The maximum number of resources that can be held in 144 * the cache. 145 * @param maxResourceBytes The maximum number of bytes of video memory 146 * that can be held in the cache. 147 */ 148 void setResourceCacheLimits(int maxResources, size_t maxResourceBytes); 149 150 /** 151 * Frees GPU created by the context. Can be called to reduce GPU memory 152 * pressure. 153 */ 154 void freeGpuResources(); 155 156 /** 157 * This method should be called whenever a GrResource is unreffed or 158 * switched from exclusive to non-exclusive. This 159 * gives the resource cache a chance to discard unneeded resources. 160 * Note: this entry point will be removed once totally ref-driven 161 * cache maintenance is implemented. 162 */ 163 void purgeCache(); 164 165 /** 166 * Purge all the unlocked resources from the cache. 167 * This entry point is mainly meant for timing texture uploads 168 * and is not defined in normal builds of Skia. 169 */ 170 void purgeAllUnlockedResources(); 171 172 /** 173 * Stores a custom resource in the cache, based on the specified key. 174 */ 175 void addResourceToCache(const GrContentKey&, GrGpuResource*); 176 177 /** 178 * Finds a resource in the cache, based on the specified key. This is intended for use in 179 * conjunction with addResourceToCache(). The return value will be NULL if not found. The 180 * caller must balance with a call to unref(). 181 */ 182 GrGpuResource* findAndRefCachedResource(const GrContentKey&); 183 184 /** 185 * Creates a new text rendering context that is optimal for the 186 * render target and the context. Caller assumes the ownership 187 * of the returned object. The returned object must be deleted 188 * before the context is destroyed. 189 */ 190 GrTextContext* createTextContext(GrRenderTarget*, 191 const SkDeviceProperties&, 192 bool enableDistanceFieldFonts); 193 194 /////////////////////////////////////////////////////////////////////////// 195 // Textures 196 197 /** 198 * Creates a new entry, based on the specified key and texture and returns it. The caller owns a 199 * ref on the returned texture which must be balanced by a call to unref. 200 * 201 * TODO: Move resizing logic out of GrContext and have the caller set the content key on the 202 * returned texture rather than take it as a param. 203 * 204 * @param params The texture params used to draw a texture may help determine 205 * the cache entry used. (e.g. different versions may exist 206 * for different wrap modes on GPUs with limited NPOT 207 * texture support). NULL implies clamp wrap modes. 208 * @param desc Description of the texture properties. 209 * @param key Key to associate with the texture. 210 * @param srcData Pointer to the pixel values. 211 * @param rowBytes The number of bytes between rows of the texture. Zero 212 * implies tightly packed rows. For compressed pixel configs, this 213 * field is ignored. 214 * @param outKey (optional) If non-NULL, we'll write the cache key we used to cacheKey. this 215 * may differ from key on GPUs that don't support tiling NPOT textures. 216 */ 217 GrTexture* createTexture(const GrTextureParams* params, 218 const GrSurfaceDesc& desc, 219 const GrContentKey& key, 220 const void* srcData, 221 size_t rowBytes, 222 GrContentKey* outKey = NULL); 223 /** 224 * Search for an entry based on key and dimensions. If found, ref it and return it. The return 225 * value will be NULL if not found. The caller must balance with a call to unref. 226 * 227 * TODO: Remove this function and do lookups generically. 228 * 229 * @param desc Description of the texture properties. 230 * @param key key to use for texture look up. 231 * @param params The texture params used to draw a texture may help determine 232 * the cache entry used. (e.g. different versions may exist 233 * for different wrap modes on GPUs with limited NPOT 234 * texture support). NULL implies clamp wrap modes. 235 */ 236 GrTexture* findAndRefTexture(const GrSurfaceDesc& desc, 237 const GrContentKey& key, 238 const GrTextureParams* params); 239 /** 240 * Determines whether a texture is in the cache. If the texture is found it 241 * will not be locked or returned. This call does not affect the priority of 242 * the texture for deletion. 243 * 244 * TODO: Remove this function and do cache checks generically. 245 */ 246 bool isTextureInCache(const GrSurfaceDesc& desc, 247 const GrContentKey& key, 248 const GrTextureParams* params) const; 249 250 /** 251 * Enum that determines how closely a returned scratch texture must match 252 * a provided GrSurfaceDesc. 253 */ 254 enum ScratchTexMatch { 255 /** 256 * Finds a texture that exactly matches the descriptor. 257 */ 258 kExact_ScratchTexMatch, 259 /** 260 * Finds a texture that approximately matches the descriptor. Will be 261 * at least as large in width and height as desc specifies. If desc 262 * specifies that texture is a render target then result will be a 263 * render target. If desc specifies a render target and doesn't set the 264 * no stencil flag then result will have a stencil. Format and aa level 265 * will always match. 266 */ 267 kApprox_ScratchTexMatch 268 }; 269 270 /** 271 * Returns a texture matching the desc. It's contents are unknown. The caller 272 * owns a ref on the returned texture and must balance with a call to unref. 273 * It is guaranteed that the same texture will not be returned in subsequent 274 * calls until all refs to the texture are dropped. 275 * 276 * Textures created by createTexture() hide the complications of 277 * tiling non-power-of-two textures on APIs that don't support this (e.g. 278 * unextended GLES2). NPOT scratch textures are not tilable on such APIs. 279 * 280 * internalFlag is a temporary workaround until changes in the internal 281 * architecture are complete. Use the default value. 282 */ 283 GrTexture* refScratchTexture(const GrSurfaceDesc&, ScratchTexMatch match, 284 bool internalFlag = false); 285 286 /** 287 * Creates a texture that is outside the cache. Does not count against 288 * cache's budget. 289 * 290 * Textures created by createTexture() hide the complications of 291 * tiling non-power-of-two textures on APIs that don't support this (e.g. 292 * unextended GLES2). NPOT uncached textures are not tilable on such APIs. 293 */ 294 GrTexture* createUncachedTexture(const GrSurfaceDesc& desc, 295 void* srcData, 296 size_t rowBytes); 297 298 /** 299 * Returns true if the specified use of an indexed texture is supported. 300 * Support may depend upon whether the texture params indicate that the 301 * texture will be tiled. Passing NULL for the texture params indicates 302 * clamp mode. 303 */ 304 bool supportsIndex8PixelConfig(const GrTextureParams*, 305 int width, 306 int height) const; 307 308 /** 309 * Return the max width or height of a texture supported by the current GPU. 310 */ 311 int getMaxTextureSize() const; 312 313 /** 314 * Temporarily override the true max texture size. Note: an override 315 * larger then the true max texture size will have no effect. 316 * This entry point is mainly meant for testing texture size dependent 317 * features and is only available if defined outside of Skia (see 318 * bleed GM. 319 */ 320 void setMaxTextureSizeOverride(int maxTextureSizeOverride); 321 322 /////////////////////////////////////////////////////////////////////////// 323 // Render targets 324 325 /** 326 * Sets the render target. 327 * @param target the render target to set. 328 */ 329 void setRenderTarget(GrRenderTarget* target) { 330 fRenderTarget.reset(SkSafeRef(target)); 331 } 332 333 /** 334 * Gets the current render target. 335 * @return the currently bound render target. 336 */ 337 const GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); } 338 GrRenderTarget* getRenderTarget() { return fRenderTarget.get(); } 339 340 /** 341 * Can the provided configuration act as a color render target? 342 */ 343 bool isConfigRenderable(GrPixelConfig config, bool withMSAA) const; 344 345 /** 346 * Return the max width or height of a render target supported by the 347 * current GPU. 348 */ 349 int getMaxRenderTargetSize() const; 350 351 /** 352 * Returns the max sample count for a render target. It will be 0 if MSAA 353 * is not supported. 354 */ 355 int getMaxSampleCount() const; 356 357 /** 358 * Returns the recommended sample count for a render target when using this 359 * context. 360 * 361 * @param config the configuration of the render target. 362 * @param dpi the display density in dots per inch. 363 * 364 * @return sample count that should be perform well and have good enough 365 * rendering quality for the display. Alternatively returns 0 if 366 * MSAA is not supported or recommended to be used by default. 367 */ 368 int getRecommendedSampleCount(GrPixelConfig config, SkScalar dpi) const; 369 370 /////////////////////////////////////////////////////////////////////////// 371 // Backend Surfaces 372 373 /** 374 * Wraps an existing texture with a GrTexture object. 375 * 376 * OpenGL: if the object is a texture Gr may change its GL texture params 377 * when it is drawn. 378 * 379 * @param desc description of the object to create. 380 * 381 * @return GrTexture object or NULL on failure. 382 */ 383 GrTexture* wrapBackendTexture(const GrBackendTextureDesc& desc); 384 385 /** 386 * Wraps an existing render target with a GrRenderTarget object. It is 387 * similar to wrapBackendTexture but can be used to draw into surfaces 388 * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that 389 * the client will resolve to a texture). 390 * 391 * @param desc description of the object to create. 392 * 393 * @return GrTexture object or NULL on failure. 394 */ 395 GrRenderTarget* wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc); 396 397 /////////////////////////////////////////////////////////////////////////// 398 // Clip state 399 /** 400 * Gets the current clip. 401 * @return the current clip. 402 */ 403 const GrClipData* getClip() const { return fClip; } 404 405 /** 406 * Sets the clip. 407 * @param clipData the clip to set. 408 */ 409 void setClip(const GrClipData* clipData) { fClip = clipData; } 410 411 /////////////////////////////////////////////////////////////////////////// 412 // Draws 413 414 /** 415 * Clear the entire or rect of the render target, ignoring any clips. 416 * @param rect the rect to clear or the whole thing if rect is NULL. 417 * @param color the color to clear to. 418 * @param canIgnoreRect allows partial clears to be converted to whole 419 * clears on platforms for which that is cheap 420 * @param target The render target to clear. 421 */ 422 void clear(const SkIRect* rect, GrColor color, bool canIgnoreRect, GrRenderTarget* target); 423 424 /** 425 * Draw everywhere (respecting the clip) with the paint. 426 */ 427 void drawPaint(const GrPaint&, const SkMatrix& viewMatrix); 428 429 /** 430 * Draw the rect using a paint. 431 * @param paint describes how to color pixels. 432 * @param viewMatrix transformation matrix 433 * @param strokeInfo the stroke information (width, join, cap), and. 434 * the dash information (intervals, count, phase). 435 * If strokeInfo == NULL, then the rect is filled. 436 * Otherwise, if stroke width == 0, then the stroke 437 * is always a single pixel thick, else the rect is 438 * mitered/beveled stroked based on stroke width. 439 * The rects coords are used to access the paint (through texture matrix) 440 */ 441 void drawRect(const GrPaint& paint, 442 const SkMatrix& viewMatrix, 443 const SkRect&, 444 const GrStrokeInfo* strokeInfo = NULL); 445 446 /** 447 * Maps a rectangle of shader coordinates to a rectangle and draws that rectangle 448 * 449 * @param paint describes how to color pixels. 450 * @param viewMatrix transformation matrix which applies to rectToDraw 451 * @param rectToDraw the rectangle to draw 452 * @param localRect the rectangle of shader coordinates applied to rectToDraw 453 * @param localMatrix an optional matrix to transform the shader coordinates before applying 454 * to rectToDraw 455 */ 456 void drawNonAARectToRect(const GrPaint& paint, 457 const SkMatrix& viewMatrix, 458 const SkRect& rectToDraw, 459 const SkRect& localRect, 460 const SkMatrix* localMatrix = NULL); 461 462 /** 463 * Draws a non-AA rect with paint and a localMatrix 464 */ 465 void drawNonAARectWithLocalMatrix(const GrPaint& paint, 466 const SkMatrix& viewMatrix, 467 const SkRect& rect, 468 const SkMatrix& localMatrix) { 469 this->drawNonAARectToRect(paint, viewMatrix, rect, rect, &localMatrix); 470 } 471 472 /** 473 * Draw a roundrect using a paint. 474 * 475 * @param paint describes how to color pixels. 476 * @param viewMatrix transformation matrix 477 * @param rrect the roundrect to draw 478 * @param strokeInfo the stroke information (width, join, cap) and 479 * the dash information (intervals, count, phase). 480 */ 481 void drawRRect(const GrPaint&, const SkMatrix& viewMatrix, const SkRRect& rrect, 482 const GrStrokeInfo&); 483 484 /** 485 * Shortcut for drawing an SkPath consisting of nested rrects using a paint. 486 * Does not support stroking. The result is undefined if outer does not contain 487 * inner. 488 * 489 * @param paint describes how to color pixels. 490 * @param viewMatrix transformation matrix 491 * @param outer the outer roundrect 492 * @param inner the inner roundrect 493 */ 494 void drawDRRect(const GrPaint&, const SkMatrix& viewMatrix, const SkRRect& outer, 495 const SkRRect& inner); 496 497 498 /** 499 * Draws a path. 500 * 501 * @param paint describes how to color pixels. 502 * @param viewMatrix transformation matrix 503 * @param path the path to draw 504 * @param strokeInfo the stroke information (width, join, cap) and 505 * the dash information (intervals, count, phase). 506 */ 507 void drawPath(const GrPaint&, const SkMatrix& viewMatrix, const SkPath&, const GrStrokeInfo&); 508 509 /** 510 * Draws vertices with a paint. 511 * 512 * @param paint describes how to color pixels. 513 * @param viewMatrix transformation matrix 514 * @param primitiveType primitives type to draw. 515 * @param vertexCount number of vertices. 516 * @param positions array of vertex positions, required. 517 * @param texCoords optional array of texture coordinates used 518 * to access the paint. 519 * @param colors optional array of per-vertex colors, supercedes 520 * the paint's color field. 521 * @param indices optional array of indices. If NULL vertices 522 * are drawn non-indexed. 523 * @param indexCount if indices is non-null then this is the 524 * number of indices. 525 */ 526 void drawVertices(const GrPaint& paint, 527 const SkMatrix& viewMatrix, 528 GrPrimitiveType primitiveType, 529 int vertexCount, 530 const SkPoint positions[], 531 const SkPoint texs[], 532 const GrColor colors[], 533 const uint16_t indices[], 534 int indexCount); 535 536 /** 537 * Draws an oval. 538 * 539 * @param paint describes how to color pixels. 540 * @param viewMatrix transformation matrix 541 * @param oval the bounding rect of the oval. 542 * @param strokeInfo the stroke information (width, join, cap) and 543 * the dash information (intervals, count, phase). 544 */ 545 void drawOval(const GrPaint& paint, 546 const SkMatrix& viewMatrix, 547 const SkRect& oval, 548 const GrStrokeInfo& strokeInfo); 549 550 /////////////////////////////////////////////////////////////////////////// 551 // Misc. 552 553 /** 554 * Flags that affect flush() behavior. 555 */ 556 enum FlushBits { 557 /** 558 * A client may reach a point where it has partially rendered a frame 559 * through a GrContext that it knows the user will never see. This flag 560 * causes the flush to skip submission of deferred content to the 3D API 561 * during the flush. 562 */ 563 kDiscard_FlushBit = 0x2, 564 }; 565 566 /** 567 * Call to ensure all drawing to the context has been issued to the 568 * underlying 3D API. 569 * @param flagsBitfield flags that control the flushing behavior. See 570 * FlushBits. 571 */ 572 void flush(int flagsBitfield = 0); 573 574 /** 575 * These flags can be used with the read/write pixels functions below. 576 */ 577 enum PixelOpsFlags { 578 /** The GrContext will not be flushed before the surface read or write. This means that 579 the read or write may occur before previous draws have executed. */ 580 kDontFlush_PixelOpsFlag = 0x1, 581 /** Any surface writes should be flushed to the backend 3D API after the surface operation 582 is complete */ 583 kFlushWrites_PixelOp = 0x2, 584 /** The src for write or dst read is unpremultiplied. This is only respected if both the 585 config src and dst configs are an RGBA/BGRA 8888 format. */ 586 kUnpremul_PixelOpsFlag = 0x4, 587 }; 588 589 /** 590 * Reads a rectangle of pixels from a render target. 591 * @param target the render target to read from. 592 * @param left left edge of the rectangle to read (inclusive) 593 * @param top top edge of the rectangle to read (inclusive) 594 * @param width width of rectangle to read in pixels. 595 * @param height height of rectangle to read in pixels. 596 * @param config the pixel config of the destination buffer 597 * @param buffer memory to read the rectangle into. 598 * @param rowBytes number of bytes bewtween consecutive rows. Zero means rows are tightly 599 * packed. 600 * @param pixelOpsFlags see PixelOpsFlags enum above. 601 * 602 * @return true if the read succeeded, false if not. The read can fail because of an unsupported 603 * pixel config or because no render target is currently set and NULL was passed for 604 * target. 605 */ 606 bool readRenderTargetPixels(GrRenderTarget* target, 607 int left, int top, int width, int height, 608 GrPixelConfig config, void* buffer, 609 size_t rowBytes = 0, 610 uint32_t pixelOpsFlags = 0); 611 612 /** 613 * Writes a rectangle of pixels to a surface. 614 * @param surface the surface to write to. 615 * @param left left edge of the rectangle to write (inclusive) 616 * @param top top edge of the rectangle to write (inclusive) 617 * @param width width of rectangle to write in pixels. 618 * @param height height of rectangle to write in pixels. 619 * @param config the pixel config of the source buffer 620 * @param buffer memory to read pixels from 621 * @param rowBytes number of bytes between consecutive rows. Zero 622 * means rows are tightly packed. 623 * @param pixelOpsFlags see PixelOpsFlags enum above. 624 * @return true if the write succeeded, false if not. The write can fail because of an 625 * unsupported combination of surface and src configs. 626 */ 627 bool writeSurfacePixels(GrSurface* surface, 628 int left, int top, int width, int height, 629 GrPixelConfig config, const void* buffer, 630 size_t rowBytes, 631 uint32_t pixelOpsFlags = 0); 632 633 /** 634 * Copies a rectangle of texels from src to dst. 635 * bounds. 636 * @param dst the surface to copy to. 637 * @param src the surface to copy from. 638 * @param srcRect the rectangle of the src that should be copied. 639 * @param dstPoint the translation applied when writing the srcRect's pixels to the dst. 640 * @param pixelOpsFlags see PixelOpsFlags enum above. (kUnpremul_PixelOpsFlag is not allowed). 641 */ 642 void copySurface(GrSurface* dst, 643 GrSurface* src, 644 const SkIRect& srcRect, 645 const SkIPoint& dstPoint, 646 uint32_t pixelOpsFlags = 0); 647 648 /** Helper that copies the whole surface but fails when the two surfaces are not identically 649 sized. */ 650 bool copySurface(GrSurface* dst, GrSurface* src) { 651 if (NULL == dst || NULL == src || dst->width() != src->width() || 652 dst->height() != src->height()) { 653 return false; 654 } 655 this->copySurface(dst, src, SkIRect::MakeWH(dst->width(), dst->height()), 656 SkIPoint::Make(0,0)); 657 return true; 658 } 659 660 /** 661 * After this returns any pending writes to the surface will have been issued to the backend 3D API. 662 */ 663 void flushSurfaceWrites(GrSurface* surface); 664 665 /** 666 * Equivalent to flushSurfaceWrites but also performs MSAA resolve if necessary. This call is 667 * used to make the surface contents available to be read in the backend 3D API, usually for a 668 * compositing step external to Skia. 669 * 670 * It is not necessary to call this before reading the render target via Skia/GrContext. 671 * GrContext will detect when it must perform a resolve before reading pixels back from the 672 * surface or using it as a texture. 673 */ 674 void prepareSurfaceForExternalRead(GrSurface*); 675 676 /** 677 * Provides a perfomance hint that the render target's contents are allowed 678 * to become undefined. 679 */ 680 void discardRenderTarget(GrRenderTarget*); 681 682#ifdef SK_DEVELOPER 683 void dumpFontCache() const; 684#endif 685 686 /////////////////////////////////////////////////////////////////////////// 687 // Helpers 688 689 class AutoRenderTarget : public ::SkNoncopyable { 690 public: 691 AutoRenderTarget(GrContext* context, GrRenderTarget* target) { 692 fPrevTarget = context->getRenderTarget(); 693 SkSafeRef(fPrevTarget); 694 context->setRenderTarget(target); 695 fContext = context; 696 } 697 AutoRenderTarget(GrContext* context) { 698 fPrevTarget = context->getRenderTarget(); 699 SkSafeRef(fPrevTarget); 700 fContext = context; 701 } 702 ~AutoRenderTarget() { 703 if (fContext) { 704 fContext->setRenderTarget(fPrevTarget); 705 } 706 SkSafeUnref(fPrevTarget); 707 } 708 private: 709 GrContext* fContext; 710 GrRenderTarget* fPrevTarget; 711 }; 712 713 class AutoClip : public ::SkNoncopyable { 714 public: 715 // This enum exists to require a caller of the constructor to acknowledge that the clip will 716 // initially be wide open. It also could be extended if there are other desirable initial 717 // clip states. 718 enum InitialClip { 719 kWideOpen_InitialClip, 720 }; 721 722 AutoClip(GrContext* context, InitialClip SkDEBUGCODE(initialState)) 723 : fContext(context) { 724 SkASSERT(kWideOpen_InitialClip == initialState); 725 fNewClipData.fClipStack = &fNewClipStack; 726 727 fOldClip = context->getClip(); 728 context->setClip(&fNewClipData); 729 } 730 731 AutoClip(GrContext* context, const SkRect& newClipRect) 732 : fContext(context) 733 , fNewClipStack(newClipRect) { 734 fNewClipData.fClipStack = &fNewClipStack; 735 736 fOldClip = fContext->getClip(); 737 fContext->setClip(&fNewClipData); 738 } 739 740 ~AutoClip() { 741 if (fContext) { 742 fContext->setClip(fOldClip); 743 } 744 } 745 private: 746 GrContext* fContext; 747 const GrClipData* fOldClip; 748 749 SkClipStack fNewClipStack; 750 GrClipData fNewClipData; 751 }; 752 753 class AutoWideOpenIdentityDraw { 754 public: 755 AutoWideOpenIdentityDraw(GrContext* ctx, GrRenderTarget* rt) 756 : fAutoClip(ctx, AutoClip::kWideOpen_InitialClip) 757 , fAutoRT(ctx, rt) { 758 } 759 760 private: 761 AutoClip fAutoClip; 762 AutoRenderTarget fAutoRT; 763 }; 764 765 /////////////////////////////////////////////////////////////////////////// 766 // Functions intended for internal use only. 767 GrGpu* getGpu() { return fGpu; } 768 const GrGpu* getGpu() const { return fGpu; } 769 GrFontCache* getFontCache() { return fFontCache; } 770 GrLayerCache* getLayerCache() { return fLayerCache.get(); } 771 GrDrawTarget* getTextTarget(); 772 const GrIndexBuffer* getQuadIndexBuffer() const; 773 GrAARectRenderer* getAARectRenderer() { return fAARectRenderer; } 774 GrResourceCache2* getResourceCache2() { return fResourceCache2; } 775 776 // Called by tests that draw directly to the context via GrDrawTarget 777 void getTestTarget(GrTestTarget*); 778 779 void addGpuTraceMarker(const GrGpuTraceMarker* marker); 780 void removeGpuTraceMarker(const GrGpuTraceMarker* marker); 781 782 GrPathRenderer* getPathRenderer( 783 const GrDrawTarget* target, 784 const GrPipelineBuilder*, 785 const SkMatrix& viewMatrix, 786 const SkPath& path, 787 const SkStrokeRec& stroke, 788 bool allowSW, 789 GrPathRendererChain::DrawType drawType = GrPathRendererChain::kColor_DrawType, 790 GrPathRendererChain::StencilSupport* stencilSupport = NULL); 791 792 /** 793 * This returns a copy of the the GrContext::Options that was passed to the 794 * constructor of this class. 795 */ 796 const Options& getOptions() const { return fOptions; } 797 798#if GR_CACHE_STATS 799 void printCacheStats() const; 800#endif 801 802 class GPUStats { 803 public: 804#if GR_GPU_STATS 805 GPUStats() { this->reset(); } 806 807 void reset() { fRenderTargetBinds = 0; fShaderCompilations = 0; } 808 809 int renderTargetBinds() const { return fRenderTargetBinds; } 810 void incRenderTargetBinds() { fRenderTargetBinds++; } 811 int shaderCompilations() const { return fShaderCompilations; } 812 void incShaderCompilations() { fShaderCompilations++; } 813 private: 814 int fRenderTargetBinds; 815 int fShaderCompilations; 816#else 817 void incRenderTargetBinds() {} 818 void incShaderCompilations() {} 819#endif 820 }; 821 822#if GR_GPU_STATS 823 const GPUStats* gpuStats() const; 824#endif 825 826private: 827 GrGpu* fGpu; 828 SkAutoTUnref<GrRenderTarget> fRenderTarget; 829 const GrClipData* fClip; // TODO: make this ref counted 830 831 GrResourceCache2* fResourceCache2; 832 GrFontCache* fFontCache; 833 SkAutoTDelete<GrLayerCache> fLayerCache; 834 835 GrPathRendererChain* fPathRendererChain; 836 GrSoftwarePathRenderer* fSoftwarePathRenderer; 837 838 GrVertexBufferAllocPool* fDrawBufferVBAllocPool; 839 GrIndexBufferAllocPool* fDrawBufferIBAllocPool; 840 GrInOrderDrawBuffer* fDrawBuffer; 841 842 // Set by OverbudgetCB() to request that GrContext flush before exiting a draw. 843 bool fFlushToReduceCacheSize; 844 GrAARectRenderer* fAARectRenderer; 845 GrOvalRenderer* fOvalRenderer; 846 847 bool fDidTestPMConversions; 848 int fPMToUPMConversion; 849 int fUPMToPMConversion; 850 851 struct CleanUpData { 852 PFCleanUpFunc fFunc; 853 void* fInfo; 854 }; 855 856 SkTDArray<CleanUpData> fCleanUpData; 857 858 int fMaxTextureSizeOverride; 859 860 const Options fOptions; 861 862 GrContext(const Options&); // init must be called after the constructor. 863 bool init(GrBackend, GrBackendContext); 864 void initMockContext(); 865 void initCommon(); 866 867 void setupDrawBuffer(); 868 869 class AutoCheckFlush; 870 // Sets the paint and returns the target to draw into. This function is overloaded to either 871 // take a GrDrawState, GrPaint, and AutoCheckFlush, or JUST an AutoCheckFlush 872 GrDrawTarget* prepareToDraw(GrPipelineBuilder*, const GrPaint* paint, const AutoCheckFlush*); 873 874 void internalDrawPath(GrDrawTarget*, 875 GrPipelineBuilder*, 876 const SkMatrix& viewMatrix, 877 GrColor, 878 bool useAA, 879 const SkPath&, 880 const GrStrokeInfo&); 881 882 // TODO: Move this out of GrContext. 883 GrTexture* createResizedTexture(const GrSurfaceDesc&, 884 const GrContentKey& origKey, 885 const void* srcData, 886 size_t rowBytes, 887 bool filter); 888 889 /** 890 * These functions create premul <-> unpremul effects if it is possible to generate a pair 891 * of effects that make a readToUPM->writeToPM->readToUPM cycle invariant. Otherwise, they 892 * return NULL. 893 */ 894 const GrFragmentProcessor* createPMToUPMEffect(GrTexture*, bool swapRAndB, const SkMatrix&); 895 const GrFragmentProcessor* createUPMToPMEffect(GrTexture*, bool swapRAndB, const SkMatrix&); 896 897 /** 898 * This callback allows the resource cache to callback into the GrContext 899 * when the cache is still over budget after a purge. 900 */ 901 static void OverBudgetCB(void* data); 902 903 typedef SkRefCnt INHERITED; 904}; 905 906#endif 907