Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright 2010 Google Inc. |
michael@0 | 3 | * |
michael@0 | 4 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 5 | * found in the LICENSE file. |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | #ifndef GrContext_DEFINED |
michael@0 | 9 | #define GrContext_DEFINED |
michael@0 | 10 | |
michael@0 | 11 | #include "GrClipData.h" |
michael@0 | 12 | #include "GrColor.h" |
michael@0 | 13 | #include "GrPaint.h" |
michael@0 | 14 | #include "GrPathRendererChain.h" |
michael@0 | 15 | #include "GrPoint.h" |
michael@0 | 16 | #include "GrRenderTarget.h" |
michael@0 | 17 | #include "GrTexture.h" |
michael@0 | 18 | #include "SkMatrix.h" |
michael@0 | 19 | #include "SkTypes.h" |
michael@0 | 20 | |
michael@0 | 21 | class GrAARectRenderer; |
michael@0 | 22 | class GrAutoScratchTexture; |
michael@0 | 23 | class GrDrawState; |
michael@0 | 24 | class GrDrawTarget; |
michael@0 | 25 | class GrEffect; |
michael@0 | 26 | class GrFontCache; |
michael@0 | 27 | class GrGpu; |
michael@0 | 28 | class GrIndexBuffer; |
michael@0 | 29 | class GrIndexBufferAllocPool; |
michael@0 | 30 | class GrInOrderDrawBuffer; |
michael@0 | 31 | class GrOvalRenderer; |
michael@0 | 32 | class GrPath; |
michael@0 | 33 | class GrPathRenderer; |
michael@0 | 34 | class GrResourceEntry; |
michael@0 | 35 | class GrResourceCache; |
michael@0 | 36 | class GrStencilBuffer; |
michael@0 | 37 | class GrTestTarget; |
michael@0 | 38 | class GrTextureParams; |
michael@0 | 39 | class GrVertexBuffer; |
michael@0 | 40 | class GrVertexBufferAllocPool; |
michael@0 | 41 | class GrSoftwarePathRenderer; |
michael@0 | 42 | class SkStrokeRec; |
michael@0 | 43 | |
michael@0 | 44 | class SK_API GrContext : public SkRefCnt { |
michael@0 | 45 | public: |
michael@0 | 46 | SK_DECLARE_INST_COUNT(GrContext) |
michael@0 | 47 | |
michael@0 | 48 | /** |
michael@0 | 49 | * Creates a GrContext for a backend context. |
michael@0 | 50 | */ |
michael@0 | 51 | static GrContext* Create(GrBackend, GrBackendContext); |
michael@0 | 52 | |
michael@0 | 53 | virtual ~GrContext(); |
michael@0 | 54 | |
michael@0 | 55 | /** |
michael@0 | 56 | * The GrContext normally assumes that no outsider is setting state |
michael@0 | 57 | * within the underlying 3D API's context/device/whatever. This call informs |
michael@0 | 58 | * the context that the state was modified and it should resend. Shouldn't |
michael@0 | 59 | * be called frequently for good performance. |
michael@0 | 60 | * The flag bits, state, is dpendent on which backend is used by the |
michael@0 | 61 | * context, either GL or D3D (possible in future). |
michael@0 | 62 | */ |
michael@0 | 63 | void resetContext(uint32_t state = kAll_GrBackendState); |
michael@0 | 64 | |
michael@0 | 65 | /** |
michael@0 | 66 | * Callback function to allow classes to cleanup on GrContext destruction. |
michael@0 | 67 | * The 'info' field is filled in with the 'info' passed to addCleanUp. |
michael@0 | 68 | */ |
michael@0 | 69 | typedef void (*PFCleanUpFunc)(const GrContext* context, void* info); |
michael@0 | 70 | |
michael@0 | 71 | /** |
michael@0 | 72 | * Add a function to be called from within GrContext's destructor. |
michael@0 | 73 | * This gives classes a chance to free resources held on a per context basis. |
michael@0 | 74 | * The 'info' parameter will be stored and passed to the callback function. |
michael@0 | 75 | */ |
michael@0 | 76 | void addCleanUp(PFCleanUpFunc cleanUp, void* info) { |
michael@0 | 77 | CleanUpData* entry = fCleanUpData.push(); |
michael@0 | 78 | |
michael@0 | 79 | entry->fFunc = cleanUp; |
michael@0 | 80 | entry->fInfo = info; |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | /** |
michael@0 | 84 | * Abandons all GPU resources, assumes 3D API state is unknown. Call this |
michael@0 | 85 | * if you have lost the associated GPU context, and thus internal texture, |
michael@0 | 86 | * buffer, etc. references/IDs are now invalid. Should be called even when |
michael@0 | 87 | * GrContext is no longer going to be used for two reasons: |
michael@0 | 88 | * 1) ~GrContext will not try to free the objects in the 3D API. |
michael@0 | 89 | * 2) If you've created GrResources that outlive the GrContext they will |
michael@0 | 90 | * be marked as invalid (GrResource::isValid()) and won't attempt to |
michael@0 | 91 | * free their underlying resource in the 3D API. |
michael@0 | 92 | * Content drawn since the last GrContext::flush() may be lost. |
michael@0 | 93 | */ |
michael@0 | 94 | void contextLost(); |
michael@0 | 95 | |
michael@0 | 96 | /** |
michael@0 | 97 | * Similar to contextLost, but makes no attempt to reset state. |
michael@0 | 98 | * Use this method when GrContext destruction is pending, but |
michael@0 | 99 | * the graphics context is destroyed first. |
michael@0 | 100 | */ |
michael@0 | 101 | void contextDestroyed(); |
michael@0 | 102 | |
michael@0 | 103 | /** |
michael@0 | 104 | * Frees GPU created by the context. Can be called to reduce GPU memory |
michael@0 | 105 | * pressure. |
michael@0 | 106 | */ |
michael@0 | 107 | void freeGpuResources(); |
michael@0 | 108 | |
michael@0 | 109 | /** |
michael@0 | 110 | * Returns the number of bytes of GPU memory hosted by the texture cache. |
michael@0 | 111 | */ |
michael@0 | 112 | size_t getGpuTextureCacheBytes() const; |
michael@0 | 113 | |
michael@0 | 114 | /////////////////////////////////////////////////////////////////////////// |
michael@0 | 115 | // Textures |
michael@0 | 116 | |
michael@0 | 117 | /** |
michael@0 | 118 | * Creates a new entry, based on the specified key and texture and returns it. The caller owns a |
michael@0 | 119 | * ref on the returned texture which must be balanced by a call to unref. |
michael@0 | 120 | * |
michael@0 | 121 | * @param params The texture params used to draw a texture may help determine |
michael@0 | 122 | * the cache entry used. (e.g. different versions may exist |
michael@0 | 123 | * for different wrap modes on GPUs with limited NPOT |
michael@0 | 124 | * texture support). NULL implies clamp wrap modes. |
michael@0 | 125 | * @param desc Description of the texture properties. |
michael@0 | 126 | * @param cacheID Cache-specific properties (e.g., texture gen ID) |
michael@0 | 127 | * @param srcData Pointer to the pixel values. |
michael@0 | 128 | * @param rowBytes The number of bytes between rows of the texture. Zero |
michael@0 | 129 | * implies tightly packed rows. |
michael@0 | 130 | * @param cacheKey (optional) If non-NULL, we'll write the cache key we used to cacheKey. |
michael@0 | 131 | */ |
michael@0 | 132 | GrTexture* createTexture(const GrTextureParams* params, |
michael@0 | 133 | const GrTextureDesc& desc, |
michael@0 | 134 | const GrCacheID& cacheID, |
michael@0 | 135 | void* srcData, |
michael@0 | 136 | size_t rowBytes, |
michael@0 | 137 | GrResourceKey* cacheKey = NULL); |
michael@0 | 138 | |
michael@0 | 139 | /** |
michael@0 | 140 | * Search for an entry based on key and dimensions. If found, ref it and return it. The return |
michael@0 | 141 | * value will be NULL if not found. The caller must balance with a call to unref. |
michael@0 | 142 | * |
michael@0 | 143 | * @param desc Description of the texture properties. |
michael@0 | 144 | * @param cacheID Cache-specific properties (e.g., texture gen ID) |
michael@0 | 145 | * @param params The texture params used to draw a texture may help determine |
michael@0 | 146 | * the cache entry used. (e.g. different versions may exist |
michael@0 | 147 | * for different wrap modes on GPUs with limited NPOT |
michael@0 | 148 | * texture support). NULL implies clamp wrap modes. |
michael@0 | 149 | */ |
michael@0 | 150 | GrTexture* findAndRefTexture(const GrTextureDesc& desc, |
michael@0 | 151 | const GrCacheID& cacheID, |
michael@0 | 152 | const GrTextureParams* params); |
michael@0 | 153 | /** |
michael@0 | 154 | * Determines whether a texture is in the cache. If the texture is found it |
michael@0 | 155 | * will not be locked or returned. This call does not affect the priority of |
michael@0 | 156 | * the texture for deletion. |
michael@0 | 157 | */ |
michael@0 | 158 | bool isTextureInCache(const GrTextureDesc& desc, |
michael@0 | 159 | const GrCacheID& cacheID, |
michael@0 | 160 | const GrTextureParams* params) const; |
michael@0 | 161 | |
michael@0 | 162 | /** |
michael@0 | 163 | * Enum that determines how closely a returned scratch texture must match |
michael@0 | 164 | * a provided GrTextureDesc. |
michael@0 | 165 | */ |
michael@0 | 166 | enum ScratchTexMatch { |
michael@0 | 167 | /** |
michael@0 | 168 | * Finds a texture that exactly matches the descriptor. |
michael@0 | 169 | */ |
michael@0 | 170 | kExact_ScratchTexMatch, |
michael@0 | 171 | /** |
michael@0 | 172 | * Finds a texture that approximately matches the descriptor. Will be |
michael@0 | 173 | * at least as large in width and height as desc specifies. If desc |
michael@0 | 174 | * specifies that texture is a render target then result will be a |
michael@0 | 175 | * render target. If desc specifies a render target and doesn't set the |
michael@0 | 176 | * no stencil flag then result will have a stencil. Format and aa level |
michael@0 | 177 | * will always match. |
michael@0 | 178 | */ |
michael@0 | 179 | kApprox_ScratchTexMatch |
michael@0 | 180 | }; |
michael@0 | 181 | |
michael@0 | 182 | /** |
michael@0 | 183 | * Returns a texture matching the desc. It's contents are unknown. Subsequent |
michael@0 | 184 | * requests with the same descriptor are not guaranteed to return the same |
michael@0 | 185 | * texture. The same texture is guaranteed not be returned again until it is |
michael@0 | 186 | * unlocked. Call must be balanced with an unlockTexture() call. The caller |
michael@0 | 187 | * owns a ref on the returned texture and must balance with a call to unref. |
michael@0 | 188 | * |
michael@0 | 189 | * Textures created by createAndLockTexture() hide the complications of |
michael@0 | 190 | * tiling non-power-of-two textures on APIs that don't support this (e.g. |
michael@0 | 191 | * unextended GLES2). Tiling a NPOT texture created by lockScratchTexture on |
michael@0 | 192 | * such an API will create gaps in the tiling pattern. This includes clamp |
michael@0 | 193 | * mode. (This may be addressed in a future update.) |
michael@0 | 194 | */ |
michael@0 | 195 | GrTexture* lockAndRefScratchTexture(const GrTextureDesc&, ScratchTexMatch match); |
michael@0 | 196 | |
michael@0 | 197 | /** |
michael@0 | 198 | * When done with an entry, call unlockScratchTexture(entry) on it, which returns |
michael@0 | 199 | * it to the cache, where it may be purged. This does not unref the texture. |
michael@0 | 200 | */ |
michael@0 | 201 | void unlockScratchTexture(GrTexture* texture); |
michael@0 | 202 | |
michael@0 | 203 | /** |
michael@0 | 204 | * This method should be called whenever a GrTexture is unreffed or |
michael@0 | 205 | * switched from exclusive to non-exclusive. This |
michael@0 | 206 | * gives the resource cache a chance to discard unneeded textures. |
michael@0 | 207 | * Note: this entry point will be removed once totally ref-driven |
michael@0 | 208 | * cache maintenance is implemented |
michael@0 | 209 | */ |
michael@0 | 210 | void purgeCache(); |
michael@0 | 211 | |
michael@0 | 212 | /** |
michael@0 | 213 | * Purge all the unlocked resources from the cache. |
michael@0 | 214 | * This entry point is mainly meant for timing texture uploads |
michael@0 | 215 | * and is not defined in normal builds of Skia. |
michael@0 | 216 | */ |
michael@0 | 217 | void purgeAllUnlockedResources(); |
michael@0 | 218 | |
michael@0 | 219 | /** |
michael@0 | 220 | * Creates a texture that is outside the cache. Does not count against |
michael@0 | 221 | * cache's budget. |
michael@0 | 222 | */ |
michael@0 | 223 | GrTexture* createUncachedTexture(const GrTextureDesc& desc, |
michael@0 | 224 | void* srcData, |
michael@0 | 225 | size_t rowBytes); |
michael@0 | 226 | |
michael@0 | 227 | /** |
michael@0 | 228 | * Returns true if the specified use of an indexed texture is supported. |
michael@0 | 229 | * Support may depend upon whether the texture params indicate that the |
michael@0 | 230 | * texture will be tiled. Passing NULL for the texture params indicates |
michael@0 | 231 | * clamp mode. |
michael@0 | 232 | */ |
michael@0 | 233 | bool supportsIndex8PixelConfig(const GrTextureParams*, |
michael@0 | 234 | int width, |
michael@0 | 235 | int height) const; |
michael@0 | 236 | |
michael@0 | 237 | /** |
michael@0 | 238 | * Return the current texture cache limits. |
michael@0 | 239 | * |
michael@0 | 240 | * @param maxTextures If non-null, returns maximum number of textures that |
michael@0 | 241 | * can be held in the cache. |
michael@0 | 242 | * @param maxTextureBytes If non-null, returns maximum number of bytes of |
michael@0 | 243 | * texture memory that can be held in the cache. |
michael@0 | 244 | */ |
michael@0 | 245 | void getTextureCacheLimits(int* maxTextures, size_t* maxTextureBytes) const; |
michael@0 | 246 | |
michael@0 | 247 | /** |
michael@0 | 248 | * Specify the texture cache limits. If the current cache exceeds either |
michael@0 | 249 | * of these, it will be purged (LRU) to keep the cache within these limits. |
michael@0 | 250 | * |
michael@0 | 251 | * @param maxTextures The maximum number of textures that can be held in |
michael@0 | 252 | * the cache. |
michael@0 | 253 | * @param maxTextureBytes The maximum number of bytes of texture memory |
michael@0 | 254 | * that can be held in the cache. |
michael@0 | 255 | */ |
michael@0 | 256 | void setTextureCacheLimits(int maxTextures, size_t maxTextureBytes); |
michael@0 | 257 | |
michael@0 | 258 | /** |
michael@0 | 259 | * Return the max width or height of a texture supported by the current GPU. |
michael@0 | 260 | */ |
michael@0 | 261 | int getMaxTextureSize() const; |
michael@0 | 262 | |
michael@0 | 263 | /** |
michael@0 | 264 | * Temporarily override the true max texture size. Note: an override |
michael@0 | 265 | * larger then the true max texture size will have no effect. |
michael@0 | 266 | * This entry point is mainly meant for testing texture size dependent |
michael@0 | 267 | * features and is only available if defined outside of Skia (see |
michael@0 | 268 | * bleed GM. |
michael@0 | 269 | */ |
michael@0 | 270 | void setMaxTextureSizeOverride(int maxTextureSizeOverride); |
michael@0 | 271 | |
michael@0 | 272 | /////////////////////////////////////////////////////////////////////////// |
michael@0 | 273 | // Render targets |
michael@0 | 274 | |
michael@0 | 275 | /** |
michael@0 | 276 | * Sets the render target. |
michael@0 | 277 | * @param target the render target to set. |
michael@0 | 278 | */ |
michael@0 | 279 | void setRenderTarget(GrRenderTarget* target) { |
michael@0 | 280 | fRenderTarget.reset(SkSafeRef(target)); |
michael@0 | 281 | } |
michael@0 | 282 | |
michael@0 | 283 | /** |
michael@0 | 284 | * Gets the current render target. |
michael@0 | 285 | * @return the currently bound render target. |
michael@0 | 286 | */ |
michael@0 | 287 | const GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); } |
michael@0 | 288 | GrRenderTarget* getRenderTarget() { return fRenderTarget.get(); } |
michael@0 | 289 | |
michael@0 | 290 | GrAARectRenderer* getAARectRenderer() { return fAARectRenderer; } |
michael@0 | 291 | |
michael@0 | 292 | /** |
michael@0 | 293 | * Can the provided configuration act as a color render target? |
michael@0 | 294 | */ |
michael@0 | 295 | bool isConfigRenderable(GrPixelConfig config, bool withMSAA) const; |
michael@0 | 296 | |
michael@0 | 297 | /** |
michael@0 | 298 | * Return the max width or height of a render target supported by the |
michael@0 | 299 | * current GPU. |
michael@0 | 300 | */ |
michael@0 | 301 | int getMaxRenderTargetSize() const; |
michael@0 | 302 | |
michael@0 | 303 | /** |
michael@0 | 304 | * Returns the max sample count for a render target. It will be 0 if MSAA |
michael@0 | 305 | * is not supported. |
michael@0 | 306 | */ |
michael@0 | 307 | int getMaxSampleCount() const; |
michael@0 | 308 | |
michael@0 | 309 | /** |
michael@0 | 310 | * Returns the recommended sample count for a render target when using this |
michael@0 | 311 | * context. |
michael@0 | 312 | * |
michael@0 | 313 | * @param config the configuration of the render target. |
michael@0 | 314 | * @param dpi the display density in dots per inch. |
michael@0 | 315 | * |
michael@0 | 316 | * @return sample count that should be perform well and have good enough |
michael@0 | 317 | * rendering quality for the display. Alternatively returns 0 if |
michael@0 | 318 | * MSAA is not supported or recommended to be used by default. |
michael@0 | 319 | */ |
michael@0 | 320 | int getRecommendedSampleCount(GrPixelConfig config, SkScalar dpi) const; |
michael@0 | 321 | |
michael@0 | 322 | /////////////////////////////////////////////////////////////////////////// |
michael@0 | 323 | // Backend Surfaces |
michael@0 | 324 | |
michael@0 | 325 | /** |
michael@0 | 326 | * Wraps an existing texture with a GrTexture object. |
michael@0 | 327 | * |
michael@0 | 328 | * OpenGL: if the object is a texture Gr may change its GL texture params |
michael@0 | 329 | * when it is drawn. |
michael@0 | 330 | * |
michael@0 | 331 | * @param desc description of the object to create. |
michael@0 | 332 | * |
michael@0 | 333 | * @return GrTexture object or NULL on failure. |
michael@0 | 334 | */ |
michael@0 | 335 | GrTexture* wrapBackendTexture(const GrBackendTextureDesc& desc); |
michael@0 | 336 | |
michael@0 | 337 | /** |
michael@0 | 338 | * Wraps an existing render target with a GrRenderTarget object. It is |
michael@0 | 339 | * similar to wrapBackendTexture but can be used to draw into surfaces |
michael@0 | 340 | * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that |
michael@0 | 341 | * the client will resolve to a texture). |
michael@0 | 342 | * |
michael@0 | 343 | * @param desc description of the object to create. |
michael@0 | 344 | * |
michael@0 | 345 | * @return GrTexture object or NULL on failure. |
michael@0 | 346 | */ |
michael@0 | 347 | GrRenderTarget* wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc); |
michael@0 | 348 | |
michael@0 | 349 | /////////////////////////////////////////////////////////////////////////// |
michael@0 | 350 | // Matrix state |
michael@0 | 351 | |
michael@0 | 352 | /** |
michael@0 | 353 | * Gets the current transformation matrix. |
michael@0 | 354 | * @return the current matrix. |
michael@0 | 355 | */ |
michael@0 | 356 | const SkMatrix& getMatrix() const { return fViewMatrix; } |
michael@0 | 357 | |
michael@0 | 358 | /** |
michael@0 | 359 | * Sets the transformation matrix. |
michael@0 | 360 | * @param m the matrix to set. |
michael@0 | 361 | */ |
michael@0 | 362 | void setMatrix(const SkMatrix& m) { fViewMatrix = m; } |
michael@0 | 363 | |
michael@0 | 364 | /** |
michael@0 | 365 | * Sets the current transformation matrix to identity. |
michael@0 | 366 | */ |
michael@0 | 367 | void setIdentityMatrix() { fViewMatrix.reset(); } |
michael@0 | 368 | |
michael@0 | 369 | /** |
michael@0 | 370 | * Concats the current matrix. The passed matrix is applied before the |
michael@0 | 371 | * current matrix. |
michael@0 | 372 | * @param m the matrix to concat. |
michael@0 | 373 | */ |
michael@0 | 374 | void concatMatrix(const SkMatrix& m) { fViewMatrix.preConcat(m); } |
michael@0 | 375 | |
michael@0 | 376 | |
michael@0 | 377 | /////////////////////////////////////////////////////////////////////////// |
michael@0 | 378 | // Clip state |
michael@0 | 379 | /** |
michael@0 | 380 | * Gets the current clip. |
michael@0 | 381 | * @return the current clip. |
michael@0 | 382 | */ |
michael@0 | 383 | const GrClipData* getClip() const { return fClip; } |
michael@0 | 384 | |
michael@0 | 385 | /** |
michael@0 | 386 | * Sets the clip. |
michael@0 | 387 | * @param clipData the clip to set. |
michael@0 | 388 | */ |
michael@0 | 389 | void setClip(const GrClipData* clipData) { fClip = clipData; } |
michael@0 | 390 | |
michael@0 | 391 | /////////////////////////////////////////////////////////////////////////// |
michael@0 | 392 | // Draws |
michael@0 | 393 | |
michael@0 | 394 | /** |
michael@0 | 395 | * Clear the entire or rect of the render target, ignoring any clips. |
michael@0 | 396 | * @param rect the rect to clear or the whole thing if rect is NULL. |
michael@0 | 397 | * @param color the color to clear to. |
michael@0 | 398 | * @param canIgnoreRect allows partial clears to be converted to whole |
michael@0 | 399 | * clears on platforms for which that is cheap |
michael@0 | 400 | * @param target if non-NULL, the render target to clear otherwise clear |
michael@0 | 401 | * the current render target |
michael@0 | 402 | */ |
michael@0 | 403 | void clear(const SkIRect* rect, GrColor color, bool canIgnoreRect, |
michael@0 | 404 | GrRenderTarget* target = NULL); |
michael@0 | 405 | |
michael@0 | 406 | /** |
michael@0 | 407 | * Draw everywhere (respecting the clip) with the paint. |
michael@0 | 408 | */ |
michael@0 | 409 | void drawPaint(const GrPaint& paint); |
michael@0 | 410 | |
michael@0 | 411 | /** |
michael@0 | 412 | * Draw the rect using a paint. |
michael@0 | 413 | * @param paint describes how to color pixels. |
michael@0 | 414 | * @param stroke the stroke information (width, join, cap). |
michael@0 | 415 | * If stroke == NULL, then the rect is filled. |
michael@0 | 416 | * Otherwise, if stroke width == 0, then the stroke |
michael@0 | 417 | * is always a single pixel thick, else the rect is |
michael@0 | 418 | * mitered/beveled stroked based on stroke width. |
michael@0 | 419 | * @param matrix Optional matrix applied to the rect. Applied before |
michael@0 | 420 | * context's matrix or the paint's matrix. |
michael@0 | 421 | * The rects coords are used to access the paint (through texture matrix) |
michael@0 | 422 | */ |
michael@0 | 423 | void drawRect(const GrPaint& paint, |
michael@0 | 424 | const SkRect&, |
michael@0 | 425 | const SkStrokeRec* stroke = NULL, |
michael@0 | 426 | const SkMatrix* matrix = NULL); |
michael@0 | 427 | |
michael@0 | 428 | /** |
michael@0 | 429 | * Maps a rect of local coordinates onto the a rect of destination |
michael@0 | 430 | * coordinates. Each rect can optionally be transformed. The localRect |
michael@0 | 431 | * is stretched over the dstRect. The dstRect is transformed by the |
michael@0 | 432 | * context's matrix. Additional optional matrices for both rects can be |
michael@0 | 433 | * provided by parameters. |
michael@0 | 434 | * |
michael@0 | 435 | * @param paint describes how to color pixels. |
michael@0 | 436 | * @param dstRect the destination rect to draw. |
michael@0 | 437 | * @param localRect rect of local coordinates to be mapped onto dstRect |
michael@0 | 438 | * @param dstMatrix Optional matrix to transform dstRect. Applied before context's matrix. |
michael@0 | 439 | * @param localMatrix Optional matrix to transform localRect. |
michael@0 | 440 | */ |
michael@0 | 441 | void drawRectToRect(const GrPaint& paint, |
michael@0 | 442 | const SkRect& dstRect, |
michael@0 | 443 | const SkRect& localRect, |
michael@0 | 444 | const SkMatrix* dstMatrix = NULL, |
michael@0 | 445 | const SkMatrix* localMatrix = NULL); |
michael@0 | 446 | |
michael@0 | 447 | /** |
michael@0 | 448 | * Draw a roundrect using a paint. |
michael@0 | 449 | * |
michael@0 | 450 | * @param paint describes how to color pixels. |
michael@0 | 451 | * @param rrect the roundrect to draw |
michael@0 | 452 | * @param stroke the stroke information (width, join, cap) |
michael@0 | 453 | */ |
michael@0 | 454 | void drawRRect(const GrPaint& paint, |
michael@0 | 455 | const SkRRect& rrect, |
michael@0 | 456 | const SkStrokeRec& stroke); |
michael@0 | 457 | |
michael@0 | 458 | /** |
michael@0 | 459 | * Draws a path. |
michael@0 | 460 | * |
michael@0 | 461 | * @param paint describes how to color pixels. |
michael@0 | 462 | * @param path the path to draw |
michael@0 | 463 | * @param stroke the stroke information (width, join, cap) |
michael@0 | 464 | */ |
michael@0 | 465 | void drawPath(const GrPaint& paint, const SkPath& path, const SkStrokeRec& stroke); |
michael@0 | 466 | |
michael@0 | 467 | /** |
michael@0 | 468 | * Draws vertices with a paint. |
michael@0 | 469 | * |
michael@0 | 470 | * @param paint describes how to color pixels. |
michael@0 | 471 | * @param primitiveType primitives type to draw. |
michael@0 | 472 | * @param vertexCount number of vertices. |
michael@0 | 473 | * @param positions array of vertex positions, required. |
michael@0 | 474 | * @param texCoords optional array of texture coordinates used |
michael@0 | 475 | * to access the paint. |
michael@0 | 476 | * @param colors optional array of per-vertex colors, supercedes |
michael@0 | 477 | * the paint's color field. |
michael@0 | 478 | * @param indices optional array of indices. If NULL vertices |
michael@0 | 479 | * are drawn non-indexed. |
michael@0 | 480 | * @param indexCount if indices is non-null then this is the |
michael@0 | 481 | * number of indices. |
michael@0 | 482 | */ |
michael@0 | 483 | void drawVertices(const GrPaint& paint, |
michael@0 | 484 | GrPrimitiveType primitiveType, |
michael@0 | 485 | int vertexCount, |
michael@0 | 486 | const GrPoint positions[], |
michael@0 | 487 | const GrPoint texs[], |
michael@0 | 488 | const GrColor colors[], |
michael@0 | 489 | const uint16_t indices[], |
michael@0 | 490 | int indexCount); |
michael@0 | 491 | |
michael@0 | 492 | /** |
michael@0 | 493 | * Draws an oval. |
michael@0 | 494 | * |
michael@0 | 495 | * @param paint describes how to color pixels. |
michael@0 | 496 | * @param oval the bounding rect of the oval. |
michael@0 | 497 | * @param stroke the stroke information (width, style) |
michael@0 | 498 | */ |
michael@0 | 499 | void drawOval(const GrPaint& paint, |
michael@0 | 500 | const SkRect& oval, |
michael@0 | 501 | const SkStrokeRec& stroke); |
michael@0 | 502 | |
michael@0 | 503 | /////////////////////////////////////////////////////////////////////////// |
michael@0 | 504 | // Misc. |
michael@0 | 505 | |
michael@0 | 506 | /** |
michael@0 | 507 | * Flags that affect flush() behavior. |
michael@0 | 508 | */ |
michael@0 | 509 | enum FlushBits { |
michael@0 | 510 | /** |
michael@0 | 511 | * A client may reach a point where it has partially rendered a frame |
michael@0 | 512 | * through a GrContext that it knows the user will never see. This flag |
michael@0 | 513 | * causes the flush to skip submission of deferred content to the 3D API |
michael@0 | 514 | * during the flush. |
michael@0 | 515 | */ |
michael@0 | 516 | kDiscard_FlushBit = 0x2, |
michael@0 | 517 | }; |
michael@0 | 518 | |
michael@0 | 519 | /** |
michael@0 | 520 | * Call to ensure all drawing to the context has been issued to the |
michael@0 | 521 | * underlying 3D API. |
michael@0 | 522 | * @param flagsBitfield flags that control the flushing behavior. See |
michael@0 | 523 | * FlushBits. |
michael@0 | 524 | */ |
michael@0 | 525 | void flush(int flagsBitfield = 0); |
michael@0 | 526 | |
michael@0 | 527 | /** |
michael@0 | 528 | * These flags can be used with the read/write pixels functions below. |
michael@0 | 529 | */ |
michael@0 | 530 | enum PixelOpsFlags { |
michael@0 | 531 | /** The GrContext will not be flushed. This means that the read or write may occur before |
michael@0 | 532 | previous draws have executed. */ |
michael@0 | 533 | kDontFlush_PixelOpsFlag = 0x1, |
michael@0 | 534 | /** The src for write or dst read is unpremultiplied. This is only respected if both the |
michael@0 | 535 | config src and dst configs are an RGBA/BGRA 8888 format. */ |
michael@0 | 536 | kUnpremul_PixelOpsFlag = 0x2, |
michael@0 | 537 | }; |
michael@0 | 538 | |
michael@0 | 539 | /** |
michael@0 | 540 | * Reads a rectangle of pixels from a render target. |
michael@0 | 541 | * @param target the render target to read from. NULL means the current render target. |
michael@0 | 542 | * @param left left edge of the rectangle to read (inclusive) |
michael@0 | 543 | * @param top top edge of the rectangle to read (inclusive) |
michael@0 | 544 | * @param width width of rectangle to read in pixels. |
michael@0 | 545 | * @param height height of rectangle to read in pixels. |
michael@0 | 546 | * @param config the pixel config of the destination buffer |
michael@0 | 547 | * @param buffer memory to read the rectangle into. |
michael@0 | 548 | * @param rowBytes number of bytes bewtween consecutive rows. Zero means rows are tightly |
michael@0 | 549 | * packed. |
michael@0 | 550 | * @param pixelOpsFlags see PixelOpsFlags enum above. |
michael@0 | 551 | * |
michael@0 | 552 | * @return true if the read succeeded, false if not. The read can fail because of an unsupported |
michael@0 | 553 | * pixel config or because no render target is currently set and NULL was passed for |
michael@0 | 554 | * target. |
michael@0 | 555 | */ |
michael@0 | 556 | bool readRenderTargetPixels(GrRenderTarget* target, |
michael@0 | 557 | int left, int top, int width, int height, |
michael@0 | 558 | GrPixelConfig config, void* buffer, |
michael@0 | 559 | size_t rowBytes = 0, |
michael@0 | 560 | uint32_t pixelOpsFlags = 0); |
michael@0 | 561 | |
michael@0 | 562 | /** |
michael@0 | 563 | * Copy the src pixels [buffer, row bytes, pixel config] into a render target at the specified |
michael@0 | 564 | * rectangle. |
michael@0 | 565 | * @param target the render target to write into. NULL means the current render target. |
michael@0 | 566 | * @param left left edge of the rectangle to write (inclusive) |
michael@0 | 567 | * @param top top edge of the rectangle to write (inclusive) |
michael@0 | 568 | * @param width width of rectangle to write in pixels. |
michael@0 | 569 | * @param height height of rectangle to write in pixels. |
michael@0 | 570 | * @param config the pixel config of the source buffer |
michael@0 | 571 | * @param buffer memory to read the rectangle from. |
michael@0 | 572 | * @param rowBytes number of bytes between consecutive rows. Zero means rows are tightly |
michael@0 | 573 | * packed. |
michael@0 | 574 | * @param pixelOpsFlags see PixelOpsFlags enum above. |
michael@0 | 575 | * |
michael@0 | 576 | * @return true if the write succeeded, false if not. The write can fail because of an |
michael@0 | 577 | * unsupported combination of target and pixel configs. |
michael@0 | 578 | */ |
michael@0 | 579 | bool writeRenderTargetPixels(GrRenderTarget* target, |
michael@0 | 580 | int left, int top, int width, int height, |
michael@0 | 581 | GrPixelConfig config, const void* buffer, |
michael@0 | 582 | size_t rowBytes = 0, |
michael@0 | 583 | uint32_t pixelOpsFlags = 0); |
michael@0 | 584 | |
michael@0 | 585 | /** |
michael@0 | 586 | * Reads a rectangle of pixels from a texture. |
michael@0 | 587 | * @param texture the texture to read from. |
michael@0 | 588 | * @param left left edge of the rectangle to read (inclusive) |
michael@0 | 589 | * @param top top edge of the rectangle to read (inclusive) |
michael@0 | 590 | * @param width width of rectangle to read in pixels. |
michael@0 | 591 | * @param height height of rectangle to read in pixels. |
michael@0 | 592 | * @param config the pixel config of the destination buffer |
michael@0 | 593 | * @param buffer memory to read the rectangle into. |
michael@0 | 594 | * @param rowBytes number of bytes between consecutive rows. Zero means rows are tightly |
michael@0 | 595 | * packed. |
michael@0 | 596 | * @param pixelOpsFlags see PixelOpsFlags enum above. |
michael@0 | 597 | * |
michael@0 | 598 | * @return true if the read succeeded, false if not. The read can fail because of an unsupported |
michael@0 | 599 | * pixel config. |
michael@0 | 600 | */ |
michael@0 | 601 | bool readTexturePixels(GrTexture* texture, |
michael@0 | 602 | int left, int top, int width, int height, |
michael@0 | 603 | GrPixelConfig config, void* buffer, |
michael@0 | 604 | size_t rowBytes = 0, |
michael@0 | 605 | uint32_t pixelOpsFlags = 0); |
michael@0 | 606 | |
michael@0 | 607 | /** |
michael@0 | 608 | * Writes a rectangle of pixels to a texture. |
michael@0 | 609 | * @param texture the render target to read from. |
michael@0 | 610 | * @param left left edge of the rectangle to write (inclusive) |
michael@0 | 611 | * @param top top edge of the rectangle to write (inclusive) |
michael@0 | 612 | * @param width width of rectangle to write in pixels. |
michael@0 | 613 | * @param height height of rectangle to write in pixels. |
michael@0 | 614 | * @param config the pixel config of the source buffer |
michael@0 | 615 | * @param buffer memory to read pixels from |
michael@0 | 616 | * @param rowBytes number of bytes between consecutive rows. Zero |
michael@0 | 617 | * means rows are tightly packed. |
michael@0 | 618 | * @param pixelOpsFlags see PixelOpsFlags enum above. |
michael@0 | 619 | * @return true if the write succeeded, false if not. The write can fail because of an |
michael@0 | 620 | * unsupported combination of texture and pixel configs. |
michael@0 | 621 | */ |
michael@0 | 622 | bool writeTexturePixels(GrTexture* texture, |
michael@0 | 623 | int left, int top, int width, int height, |
michael@0 | 624 | GrPixelConfig config, const void* buffer, |
michael@0 | 625 | size_t rowBytes, |
michael@0 | 626 | uint32_t pixelOpsFlags = 0); |
michael@0 | 627 | |
michael@0 | 628 | |
michael@0 | 629 | /** |
michael@0 | 630 | * Copies a rectangle of texels from src to dst. The size of dst is the size of the rectangle |
michael@0 | 631 | * copied and topLeft is the position of the rect in src. The rectangle is clipped to src's |
michael@0 | 632 | * bounds. |
michael@0 | 633 | * @param src the texture to copy from. |
michael@0 | 634 | * @param dst the render target to copy to. |
michael@0 | 635 | * @param topLeft the point in src that will be copied to the top-left of dst. If NULL, |
michael@0 | 636 | * (0, 0) will be used. |
michael@0 | 637 | */ |
michael@0 | 638 | void copyTexture(GrTexture* src, GrRenderTarget* dst, const SkIPoint* topLeft = NULL); |
michael@0 | 639 | |
michael@0 | 640 | /** |
michael@0 | 641 | * Resolves a render target that has MSAA. The intermediate MSAA buffer is |
michael@0 | 642 | * down-sampled to the associated GrTexture (accessible via |
michael@0 | 643 | * GrRenderTarget::asTexture()). Any pending draws to the render target will |
michael@0 | 644 | * be executed before the resolve. |
michael@0 | 645 | * |
michael@0 | 646 | * This is only necessary when a client wants to access the object directly |
michael@0 | 647 | * using the backend API directly. GrContext will detect when it must |
michael@0 | 648 | * perform a resolve to a GrTexture used as the source of a draw or before |
michael@0 | 649 | * reading pixels back from a GrTexture or GrRenderTarget. |
michael@0 | 650 | */ |
michael@0 | 651 | void resolveRenderTarget(GrRenderTarget* target); |
michael@0 | 652 | |
michael@0 | 653 | #ifdef SK_DEVELOPER |
michael@0 | 654 | void dumpFontCache() const; |
michael@0 | 655 | #endif |
michael@0 | 656 | |
michael@0 | 657 | /////////////////////////////////////////////////////////////////////////// |
michael@0 | 658 | // Helpers |
michael@0 | 659 | |
michael@0 | 660 | class AutoRenderTarget : public ::SkNoncopyable { |
michael@0 | 661 | public: |
michael@0 | 662 | AutoRenderTarget(GrContext* context, GrRenderTarget* target) { |
michael@0 | 663 | fPrevTarget = context->getRenderTarget(); |
michael@0 | 664 | SkSafeRef(fPrevTarget); |
michael@0 | 665 | context->setRenderTarget(target); |
michael@0 | 666 | fContext = context; |
michael@0 | 667 | } |
michael@0 | 668 | AutoRenderTarget(GrContext* context) { |
michael@0 | 669 | fPrevTarget = context->getRenderTarget(); |
michael@0 | 670 | SkSafeRef(fPrevTarget); |
michael@0 | 671 | fContext = context; |
michael@0 | 672 | } |
michael@0 | 673 | ~AutoRenderTarget() { |
michael@0 | 674 | if (NULL != fContext) { |
michael@0 | 675 | fContext->setRenderTarget(fPrevTarget); |
michael@0 | 676 | } |
michael@0 | 677 | SkSafeUnref(fPrevTarget); |
michael@0 | 678 | } |
michael@0 | 679 | private: |
michael@0 | 680 | GrContext* fContext; |
michael@0 | 681 | GrRenderTarget* fPrevTarget; |
michael@0 | 682 | }; |
michael@0 | 683 | |
michael@0 | 684 | /** |
michael@0 | 685 | * Save/restore the view-matrix in the context. It can optionally adjust a paint to account |
michael@0 | 686 | * for a coordinate system change. Here is an example of how the paint param can be used: |
michael@0 | 687 | * |
michael@0 | 688 | * A GrPaint is setup with GrEffects. The stages will have access to the pre-matrix source |
michael@0 | 689 | * geometry positions when the draw is executed. Later on a decision is made to transform the |
michael@0 | 690 | * geometry to device space on the CPU. The effects now need to know that the space in which |
michael@0 | 691 | * the geometry will be specified has changed. |
michael@0 | 692 | * |
michael@0 | 693 | * Note that when restore is called (or in the destructor) the context's matrix will be |
michael@0 | 694 | * restored. However, the paint will not be restored. The caller must make a copy of the |
michael@0 | 695 | * paint if necessary. Hint: use SkTCopyOnFirstWrite if the AutoMatrix is conditionally |
michael@0 | 696 | * initialized. |
michael@0 | 697 | */ |
michael@0 | 698 | class AutoMatrix : public ::SkNoncopyable { |
michael@0 | 699 | public: |
michael@0 | 700 | AutoMatrix() : fContext(NULL) {} |
michael@0 | 701 | |
michael@0 | 702 | ~AutoMatrix() { this->restore(); } |
michael@0 | 703 | |
michael@0 | 704 | /** |
michael@0 | 705 | * Initializes by pre-concat'ing the context's current matrix with the preConcat param. |
michael@0 | 706 | */ |
michael@0 | 707 | void setPreConcat(GrContext* context, const SkMatrix& preConcat, GrPaint* paint = NULL) { |
michael@0 | 708 | SkASSERT(NULL != context); |
michael@0 | 709 | |
michael@0 | 710 | this->restore(); |
michael@0 | 711 | |
michael@0 | 712 | fContext = context; |
michael@0 | 713 | fMatrix = context->getMatrix(); |
michael@0 | 714 | this->preConcat(preConcat, paint); |
michael@0 | 715 | } |
michael@0 | 716 | |
michael@0 | 717 | /** |
michael@0 | 718 | * Sets the context's matrix to identity. Returns false if the inverse matrix is required to |
michael@0 | 719 | * update a paint but the matrix cannot be inverted. |
michael@0 | 720 | */ |
michael@0 | 721 | bool setIdentity(GrContext* context, GrPaint* paint = NULL) { |
michael@0 | 722 | SkASSERT(NULL != context); |
michael@0 | 723 | |
michael@0 | 724 | this->restore(); |
michael@0 | 725 | |
michael@0 | 726 | if (NULL != paint) { |
michael@0 | 727 | if (!paint->localCoordChangeInverse(context->getMatrix())) { |
michael@0 | 728 | return false; |
michael@0 | 729 | } |
michael@0 | 730 | } |
michael@0 | 731 | fMatrix = context->getMatrix(); |
michael@0 | 732 | fContext = context; |
michael@0 | 733 | context->setIdentityMatrix(); |
michael@0 | 734 | return true; |
michael@0 | 735 | } |
michael@0 | 736 | |
michael@0 | 737 | /** |
michael@0 | 738 | * Replaces the context's matrix with a new matrix. Returns false if the inverse matrix is |
michael@0 | 739 | * required to update a paint but the matrix cannot be inverted. |
michael@0 | 740 | */ |
michael@0 | 741 | bool set(GrContext* context, const SkMatrix& newMatrix, GrPaint* paint = NULL) { |
michael@0 | 742 | if (NULL != paint) { |
michael@0 | 743 | if (!this->setIdentity(context, paint)) { |
michael@0 | 744 | return false; |
michael@0 | 745 | } |
michael@0 | 746 | this->preConcat(newMatrix, paint); |
michael@0 | 747 | } else { |
michael@0 | 748 | this->restore(); |
michael@0 | 749 | fContext = context; |
michael@0 | 750 | fMatrix = context->getMatrix(); |
michael@0 | 751 | context->setMatrix(newMatrix); |
michael@0 | 752 | } |
michael@0 | 753 | return true; |
michael@0 | 754 | } |
michael@0 | 755 | |
michael@0 | 756 | /** |
michael@0 | 757 | * If this has been initialized then the context's matrix will be further updated by |
michael@0 | 758 | * pre-concat'ing the preConcat param. The matrix that will be restored remains unchanged. |
michael@0 | 759 | * The paint is assumed to be relative to the context's matrix at the time this call is |
michael@0 | 760 | * made, not the matrix at the time AutoMatrix was first initialized. In other words, this |
michael@0 | 761 | * performs an incremental update of the paint. |
michael@0 | 762 | */ |
michael@0 | 763 | void preConcat(const SkMatrix& preConcat, GrPaint* paint = NULL) { |
michael@0 | 764 | if (NULL != paint) { |
michael@0 | 765 | paint->localCoordChange(preConcat); |
michael@0 | 766 | } |
michael@0 | 767 | fContext->concatMatrix(preConcat); |
michael@0 | 768 | } |
michael@0 | 769 | |
michael@0 | 770 | /** |
michael@0 | 771 | * Returns false if never initialized or the inverse matrix was required to update a paint |
michael@0 | 772 | * but the matrix could not be inverted. |
michael@0 | 773 | */ |
michael@0 | 774 | bool succeeded() const { return NULL != fContext; } |
michael@0 | 775 | |
michael@0 | 776 | /** |
michael@0 | 777 | * If this has been initialized then the context's original matrix is restored. |
michael@0 | 778 | */ |
michael@0 | 779 | void restore() { |
michael@0 | 780 | if (NULL != fContext) { |
michael@0 | 781 | fContext->setMatrix(fMatrix); |
michael@0 | 782 | fContext = NULL; |
michael@0 | 783 | } |
michael@0 | 784 | } |
michael@0 | 785 | |
michael@0 | 786 | private: |
michael@0 | 787 | GrContext* fContext; |
michael@0 | 788 | SkMatrix fMatrix; |
michael@0 | 789 | }; |
michael@0 | 790 | |
michael@0 | 791 | class AutoClip : public ::SkNoncopyable { |
michael@0 | 792 | public: |
michael@0 | 793 | // This enum exists to require a caller of the constructor to acknowledge that the clip will |
michael@0 | 794 | // initially be wide open. It also could be extended if there are other desirable initial |
michael@0 | 795 | // clip states. |
michael@0 | 796 | enum InitialClip { |
michael@0 | 797 | kWideOpen_InitialClip, |
michael@0 | 798 | }; |
michael@0 | 799 | |
michael@0 | 800 | AutoClip(GrContext* context, InitialClip initialState) |
michael@0 | 801 | : fContext(context) { |
michael@0 | 802 | SkASSERT(kWideOpen_InitialClip == initialState); |
michael@0 | 803 | fNewClipData.fClipStack = &fNewClipStack; |
michael@0 | 804 | |
michael@0 | 805 | fOldClip = context->getClip(); |
michael@0 | 806 | context->setClip(&fNewClipData); |
michael@0 | 807 | } |
michael@0 | 808 | |
michael@0 | 809 | AutoClip(GrContext* context, const SkRect& newClipRect) |
michael@0 | 810 | : fContext(context) |
michael@0 | 811 | , fNewClipStack(newClipRect) { |
michael@0 | 812 | fNewClipData.fClipStack = &fNewClipStack; |
michael@0 | 813 | |
michael@0 | 814 | fOldClip = fContext->getClip(); |
michael@0 | 815 | fContext->setClip(&fNewClipData); |
michael@0 | 816 | } |
michael@0 | 817 | |
michael@0 | 818 | ~AutoClip() { |
michael@0 | 819 | if (NULL != fContext) { |
michael@0 | 820 | fContext->setClip(fOldClip); |
michael@0 | 821 | } |
michael@0 | 822 | } |
michael@0 | 823 | private: |
michael@0 | 824 | GrContext* fContext; |
michael@0 | 825 | const GrClipData* fOldClip; |
michael@0 | 826 | |
michael@0 | 827 | SkClipStack fNewClipStack; |
michael@0 | 828 | GrClipData fNewClipData; |
michael@0 | 829 | }; |
michael@0 | 830 | |
michael@0 | 831 | class AutoWideOpenIdentityDraw { |
michael@0 | 832 | public: |
michael@0 | 833 | AutoWideOpenIdentityDraw(GrContext* ctx, GrRenderTarget* rt) |
michael@0 | 834 | : fAutoClip(ctx, AutoClip::kWideOpen_InitialClip) |
michael@0 | 835 | , fAutoRT(ctx, rt) { |
michael@0 | 836 | fAutoMatrix.setIdentity(ctx); |
michael@0 | 837 | // should never fail with no paint param. |
michael@0 | 838 | SkASSERT(fAutoMatrix.succeeded()); |
michael@0 | 839 | } |
michael@0 | 840 | |
michael@0 | 841 | private: |
michael@0 | 842 | AutoClip fAutoClip; |
michael@0 | 843 | AutoRenderTarget fAutoRT; |
michael@0 | 844 | AutoMatrix fAutoMatrix; |
michael@0 | 845 | }; |
michael@0 | 846 | |
michael@0 | 847 | /////////////////////////////////////////////////////////////////////////// |
michael@0 | 848 | // Functions intended for internal use only. |
michael@0 | 849 | GrGpu* getGpu() { return fGpu; } |
michael@0 | 850 | const GrGpu* getGpu() const { return fGpu; } |
michael@0 | 851 | GrFontCache* getFontCache() { return fFontCache; } |
michael@0 | 852 | GrDrawTarget* getTextTarget(); |
michael@0 | 853 | const GrIndexBuffer* getQuadIndexBuffer() const; |
michael@0 | 854 | |
michael@0 | 855 | // Called by tests that draw directly to the context via GrDrawTarget |
michael@0 | 856 | void getTestTarget(GrTestTarget*); |
michael@0 | 857 | |
michael@0 | 858 | /** |
michael@0 | 859 | * Stencil buffers add themselves to the cache using addStencilBuffer. findStencilBuffer is |
michael@0 | 860 | * called to check the cache for a SB that matches an RT's criteria. |
michael@0 | 861 | */ |
michael@0 | 862 | void addStencilBuffer(GrStencilBuffer* sb); |
michael@0 | 863 | GrStencilBuffer* findStencilBuffer(int width, int height, int sampleCnt); |
michael@0 | 864 | |
michael@0 | 865 | GrPathRenderer* getPathRenderer( |
michael@0 | 866 | const SkPath& path, |
michael@0 | 867 | const SkStrokeRec& stroke, |
michael@0 | 868 | const GrDrawTarget* target, |
michael@0 | 869 | bool allowSW, |
michael@0 | 870 | GrPathRendererChain::DrawType drawType = GrPathRendererChain::kColor_DrawType, |
michael@0 | 871 | GrPathRendererChain::StencilSupport* stencilSupport = NULL); |
michael@0 | 872 | |
michael@0 | 873 | |
michael@0 | 874 | #if GR_CACHE_STATS |
michael@0 | 875 | void printCacheStats() const; |
michael@0 | 876 | #endif |
michael@0 | 877 | |
michael@0 | 878 | private: |
michael@0 | 879 | // Used to indicate whether a draw should be performed immediately or queued in fDrawBuffer. |
michael@0 | 880 | enum BufferedDraw { |
michael@0 | 881 | kYes_BufferedDraw, |
michael@0 | 882 | kNo_BufferedDraw, |
michael@0 | 883 | }; |
michael@0 | 884 | BufferedDraw fLastDrawWasBuffered; |
michael@0 | 885 | |
michael@0 | 886 | GrGpu* fGpu; |
michael@0 | 887 | SkMatrix fViewMatrix; |
michael@0 | 888 | SkAutoTUnref<GrRenderTarget> fRenderTarget; |
michael@0 | 889 | const GrClipData* fClip; // TODO: make this ref counted |
michael@0 | 890 | GrDrawState* fDrawState; |
michael@0 | 891 | |
michael@0 | 892 | GrResourceCache* fTextureCache; |
michael@0 | 893 | GrFontCache* fFontCache; |
michael@0 | 894 | |
michael@0 | 895 | GrPathRendererChain* fPathRendererChain; |
michael@0 | 896 | GrSoftwarePathRenderer* fSoftwarePathRenderer; |
michael@0 | 897 | |
michael@0 | 898 | GrVertexBufferAllocPool* fDrawBufferVBAllocPool; |
michael@0 | 899 | GrIndexBufferAllocPool* fDrawBufferIBAllocPool; |
michael@0 | 900 | GrInOrderDrawBuffer* fDrawBuffer; |
michael@0 | 901 | |
michael@0 | 902 | // Set by OverbudgetCB() to request that GrContext flush before exiting a draw. |
michael@0 | 903 | bool fFlushToReduceCacheSize; |
michael@0 | 904 | |
michael@0 | 905 | GrAARectRenderer* fAARectRenderer; |
michael@0 | 906 | GrOvalRenderer* fOvalRenderer; |
michael@0 | 907 | |
michael@0 | 908 | bool fDidTestPMConversions; |
michael@0 | 909 | int fPMToUPMConversion; |
michael@0 | 910 | int fUPMToPMConversion; |
michael@0 | 911 | |
michael@0 | 912 | struct CleanUpData { |
michael@0 | 913 | PFCleanUpFunc fFunc; |
michael@0 | 914 | void* fInfo; |
michael@0 | 915 | }; |
michael@0 | 916 | |
michael@0 | 917 | SkTDArray<CleanUpData> fCleanUpData; |
michael@0 | 918 | |
michael@0 | 919 | int fMaxTextureSizeOverride; |
michael@0 | 920 | |
michael@0 | 921 | GrContext(); // init must be called after the constructor. |
michael@0 | 922 | bool init(GrBackend, GrBackendContext); |
michael@0 | 923 | |
michael@0 | 924 | void setupDrawBuffer(); |
michael@0 | 925 | |
michael@0 | 926 | class AutoRestoreEffects; |
michael@0 | 927 | class AutoCheckFlush; |
michael@0 | 928 | /// Sets the paint and returns the target to draw into. The paint can be NULL in which case the |
michael@0 | 929 | /// draw state is left unmodified. |
michael@0 | 930 | GrDrawTarget* prepareToDraw(const GrPaint*, BufferedDraw, AutoRestoreEffects*, AutoCheckFlush*); |
michael@0 | 931 | |
michael@0 | 932 | void internalDrawPath(GrDrawTarget* target, bool useAA, const SkPath& path, |
michael@0 | 933 | const SkStrokeRec& stroke); |
michael@0 | 934 | |
michael@0 | 935 | GrTexture* createResizedTexture(const GrTextureDesc& desc, |
michael@0 | 936 | const GrCacheID& cacheID, |
michael@0 | 937 | void* srcData, |
michael@0 | 938 | size_t rowBytes, |
michael@0 | 939 | bool filter); |
michael@0 | 940 | |
michael@0 | 941 | // Needed so GrTexture's returnToCache helper function can call |
michael@0 | 942 | // addExistingTextureToCache |
michael@0 | 943 | friend class GrTexture; |
michael@0 | 944 | friend class GrStencilAndCoverPathRenderer; |
michael@0 | 945 | |
michael@0 | 946 | // Add an existing texture to the texture cache. This is intended solely |
michael@0 | 947 | // for use with textures released from an GrAutoScratchTexture. |
michael@0 | 948 | void addExistingTextureToCache(GrTexture* texture); |
michael@0 | 949 | |
michael@0 | 950 | /** |
michael@0 | 951 | * These functions create premul <-> unpremul effects if it is possible to generate a pair |
michael@0 | 952 | * of effects that make a readToUPM->writeToPM->readToUPM cycle invariant. Otherwise, they |
michael@0 | 953 | * return NULL. |
michael@0 | 954 | */ |
michael@0 | 955 | const GrEffectRef* createPMToUPMEffect(GrTexture* texture, |
michael@0 | 956 | bool swapRAndB, |
michael@0 | 957 | const SkMatrix& matrix); |
michael@0 | 958 | const GrEffectRef* createUPMToPMEffect(GrTexture* texture, |
michael@0 | 959 | bool swapRAndB, |
michael@0 | 960 | const SkMatrix& matrix); |
michael@0 | 961 | |
michael@0 | 962 | /** |
michael@0 | 963 | * This callback allows the resource cache to callback into the GrContext |
michael@0 | 964 | * when the cache is still overbudget after a purge. |
michael@0 | 965 | */ |
michael@0 | 966 | static bool OverbudgetCB(void* data); |
michael@0 | 967 | |
michael@0 | 968 | /** Creates a new gpu path, based on the specified path and stroke and returns it. |
michael@0 | 969 | * The caller owns a ref on the returned path which must be balanced by a call to unref. |
michael@0 | 970 | * |
michael@0 | 971 | * @param skPath the path geometry. |
michael@0 | 972 | * @param stroke the path stroke. |
michael@0 | 973 | * @return a new path or NULL if the operation is not supported by the backend. |
michael@0 | 974 | */ |
michael@0 | 975 | GrPath* createPath(const SkPath& skPath, const SkStrokeRec& stroke); |
michael@0 | 976 | |
michael@0 | 977 | typedef SkRefCnt INHERITED; |
michael@0 | 978 | }; |
michael@0 | 979 | |
michael@0 | 980 | /** |
michael@0 | 981 | * Gets and locks a scratch texture from a descriptor using either exact or approximate criteria. |
michael@0 | 982 | * Unlocks texture in the destructor. |
michael@0 | 983 | */ |
michael@0 | 984 | class GrAutoScratchTexture : public ::SkNoncopyable { |
michael@0 | 985 | public: |
michael@0 | 986 | GrAutoScratchTexture() |
michael@0 | 987 | : fContext(NULL) |
michael@0 | 988 | , fTexture(NULL) { |
michael@0 | 989 | } |
michael@0 | 990 | |
michael@0 | 991 | GrAutoScratchTexture(GrContext* context, |
michael@0 | 992 | const GrTextureDesc& desc, |
michael@0 | 993 | GrContext::ScratchTexMatch match = GrContext::kApprox_ScratchTexMatch) |
michael@0 | 994 | : fContext(NULL) |
michael@0 | 995 | , fTexture(NULL) { |
michael@0 | 996 | this->set(context, desc, match); |
michael@0 | 997 | } |
michael@0 | 998 | |
michael@0 | 999 | ~GrAutoScratchTexture() { |
michael@0 | 1000 | this->reset(); |
michael@0 | 1001 | } |
michael@0 | 1002 | |
michael@0 | 1003 | void reset() { |
michael@0 | 1004 | if (NULL != fContext && NULL != fTexture) { |
michael@0 | 1005 | fContext->unlockScratchTexture(fTexture); |
michael@0 | 1006 | fTexture->unref(); |
michael@0 | 1007 | fTexture = NULL; |
michael@0 | 1008 | } |
michael@0 | 1009 | } |
michael@0 | 1010 | |
michael@0 | 1011 | /* |
michael@0 | 1012 | * When detaching a texture we do not unlock it in the texture cache but |
michael@0 | 1013 | * we do set the returnToCache flag. In this way the texture remains |
michael@0 | 1014 | * "locked" in the texture cache until it is freed and recycled in |
michael@0 | 1015 | * GrTexture::internal_dispose. In reality, the texture has been removed |
michael@0 | 1016 | * from the cache (because this is in AutoScratchTexture) and by not |
michael@0 | 1017 | * calling unlockScratchTexture we simply don't re-add it. It will be |
michael@0 | 1018 | * reattached in GrTexture::internal_dispose. |
michael@0 | 1019 | * |
michael@0 | 1020 | * Note that the caller is assumed to accept and manage the ref to the |
michael@0 | 1021 | * returned texture. |
michael@0 | 1022 | */ |
michael@0 | 1023 | GrTexture* detach() { |
michael@0 | 1024 | if (NULL == fTexture) { |
michael@0 | 1025 | return NULL; |
michael@0 | 1026 | } |
michael@0 | 1027 | GrTexture* texture = fTexture; |
michael@0 | 1028 | fTexture = NULL; |
michael@0 | 1029 | |
michael@0 | 1030 | // This GrAutoScratchTexture has a ref from lockAndRefScratchTexture, which we give up now. |
michael@0 | 1031 | // The cache also has a ref which we are lending to the caller of detach(). When the caller |
michael@0 | 1032 | // lets go of the ref and the ref count goes to 0 internal_dispose will see this flag is |
michael@0 | 1033 | // set and re-ref the texture, thereby restoring the cache's ref. |
michael@0 | 1034 | SkASSERT(texture->getRefCnt() > 1); |
michael@0 | 1035 | texture->setFlag((GrTextureFlags) GrTexture::kReturnToCache_FlagBit); |
michael@0 | 1036 | texture->unref(); |
michael@0 | 1037 | SkASSERT(NULL != texture->getCacheEntry()); |
michael@0 | 1038 | |
michael@0 | 1039 | return texture; |
michael@0 | 1040 | } |
michael@0 | 1041 | |
michael@0 | 1042 | GrTexture* set(GrContext* context, |
michael@0 | 1043 | const GrTextureDesc& desc, |
michael@0 | 1044 | GrContext::ScratchTexMatch match = GrContext::kApprox_ScratchTexMatch) { |
michael@0 | 1045 | this->reset(); |
michael@0 | 1046 | |
michael@0 | 1047 | fContext = context; |
michael@0 | 1048 | if (NULL != fContext) { |
michael@0 | 1049 | fTexture = fContext->lockAndRefScratchTexture(desc, match); |
michael@0 | 1050 | if (NULL == fTexture) { |
michael@0 | 1051 | fContext = NULL; |
michael@0 | 1052 | } |
michael@0 | 1053 | return fTexture; |
michael@0 | 1054 | } else { |
michael@0 | 1055 | return NULL; |
michael@0 | 1056 | } |
michael@0 | 1057 | } |
michael@0 | 1058 | |
michael@0 | 1059 | GrTexture* texture() { return fTexture; } |
michael@0 | 1060 | |
michael@0 | 1061 | private: |
michael@0 | 1062 | GrContext* fContext; |
michael@0 | 1063 | GrTexture* fTexture; |
michael@0 | 1064 | }; |
michael@0 | 1065 | |
michael@0 | 1066 | #endif |