| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 2 | /* |
| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2010 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 10 | #ifndef GrContext_DEFINED |
| 11 | #define GrContext_DEFINED |
| 12 | |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 13 | #include "GrPaint.h" |
| robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 14 | #include "GrAARectRenderer.h" |
| robertphillips@google.com | a2d7148 | 2012-08-01 20:08:47 +0000 | [diff] [blame] | 15 | #include "GrClipData.h" |
| bsalomon@google.com | c287a89 | 2011-08-19 14:49:36 +0000 | [diff] [blame] | 16 | // not strictly needed but requires WK change in LayerTextureUpdaterCanvas to |
| 17 | // remove. |
| 18 | #include "GrRenderTarget.h" |
| robertphillips@google.com | a2d7148 | 2012-08-01 20:08:47 +0000 | [diff] [blame] | 19 | #include "SkClipStack.h" |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 20 | |
| senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 21 | class GrAutoScratchTexture; |
| bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 22 | class GrDrawState; |
| bsalomon@google.com | 583a1e3 | 2011-08-17 13:42:46 +0000 | [diff] [blame] | 23 | class GrDrawTarget; |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 24 | class GrFontCache; |
| bsalomon@google.com | 05ef510 | 2011-05-02 21:14:59 +0000 | [diff] [blame] | 25 | class GrGpu; |
| bsalomon@google.com | 583a1e3 | 2011-08-17 13:42:46 +0000 | [diff] [blame] | 26 | class GrIndexBuffer; |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 27 | class GrIndexBufferAllocPool; |
| 28 | class GrInOrderDrawBuffer; |
| bsalomon@google.com | 583a1e3 | 2011-08-17 13:42:46 +0000 | [diff] [blame] | 29 | class GrPathRenderer; |
| bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 30 | class GrPathRendererChain; |
| bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 31 | class GrResourceEntry; |
| 32 | class GrResourceCache; |
| bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 33 | class GrStencilBuffer; |
| bsalomon@google.com | 583a1e3 | 2011-08-17 13:42:46 +0000 | [diff] [blame] | 34 | class GrVertexBuffer; |
| bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 35 | class GrVertexBufferAllocPool; |
| robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 36 | class GrSoftwarePathRenderer; |
| bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 37 | |
| bsalomon@google.com | 9182610 | 2011-03-21 19:51:57 +0000 | [diff] [blame] | 38 | class GR_API GrContext : public GrRefCnt { |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 39 | public: |
| reed@google.com | fa35e3d | 2012-06-26 20:16:17 +0000 | [diff] [blame] | 40 | SK_DECLARE_INST_COUNT(GrContext) |
| 41 | |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 42 | /** |
| 43 | * Creates a GrContext from within a 3D context. |
| 44 | */ |
| bsalomon@google.com | 05ef510 | 2011-05-02 21:14:59 +0000 | [diff] [blame] | 45 | static GrContext* Create(GrEngine engine, |
| 46 | GrPlatform3DContext context3D); |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 47 | |
| bsalomon@google.com | c0af317 | 2012-06-15 14:10:09 +0000 | [diff] [blame] | 48 | /** |
| 49 | * Returns the number of GrContext instances for the current thread. |
| 50 | */ |
| 51 | static int GetThreadInstanceCount(); |
| 52 | |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 53 | virtual ~GrContext(); |
| 54 | |
| 55 | /** |
| 56 | * The GrContext normally assumes that no outsider is setting state |
| 57 | * within the underlying 3D API's context/device/whatever. This call informs |
| 58 | * the context that the state was modified and it should resend. Shouldn't |
| 59 | * be called frequently for good performance. |
| 60 | */ |
| 61 | void resetContext(); |
| 62 | |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 63 | /** |
| 64 | * Abandons all gpu resources, assumes 3D API state is unknown. Call this |
| 65 | * if you have lost the associated GPU context, and thus internal texture, |
| 66 | * buffer, etc. references/IDs are now invalid. Should be called even when |
| 67 | * GrContext is no longer going to be used for two reasons: |
| 68 | * 1) ~GrContext will not try to free the objects in the 3D API. |
| 69 | * 2) If you've created GrResources that outlive the GrContext they will |
| 70 | * be marked as invalid (GrResource::isValid()) and won't attempt to |
| 71 | * free their underlying resource in the 3D API. |
| 72 | * Content drawn since the last GrContext::flush() may be lost. |
| 73 | */ |
| 74 | void contextLost(); |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 75 | |
| 76 | /** |
| junov@google.com | 53a5584 | 2011-06-08 22:55:10 +0000 | [diff] [blame] | 77 | * Similar to contextLost, but makes no attempt to reset state. |
| 78 | * Use this method when GrContext destruction is pending, but |
| 79 | * the graphics context is destroyed first. |
| 80 | */ |
| 81 | void contextDestroyed(); |
| 82 | |
| 83 | /** |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 84 | * Frees gpu created by the context. Can be called to reduce GPU memory |
| 85 | * pressure. |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 86 | */ |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 87 | void freeGpuResources(); |
| 88 | |
| bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 89 | /** |
| 90 | * Returns the number of bytes of GPU memory hosted by the texture cache. |
| 91 | */ |
| 92 | size_t getGpuTextureCacheBytes() const; |
| 93 | |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 94 | /////////////////////////////////////////////////////////////////////////// |
| 95 | // Textures |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 96 | |
| 97 | /** |
| bsalomon@google.com | 1fadb20 | 2011-12-12 16:10:08 +0000 | [diff] [blame] | 98 | * Create a new entry, based on the specified key and texture, and return |
| robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 99 | * a "locked" texture. Must call be balanced with an unlockTexture() call. |
| bsalomon@google.com | 1fadb20 | 2011-12-12 16:10:08 +0000 | [diff] [blame] | 100 | * |
| bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 101 | * @param params The tex params used to draw a texture may help determine |
| 102 | * the cache entry used. (e.g. different versions may exist |
| 103 | * for different wrap modes on GPUs with limited NPOT |
| 104 | * texture support). NULL implies clamp wrap modes. |
| bsalomon@google.com | 1fadb20 | 2011-12-12 16:10:08 +0000 | [diff] [blame] | 105 | * @param desc Description of the texture properties. |
| robertphillips@google.com | 9c2ea84 | 2012-08-13 17:47:59 +0000 | [diff] [blame] | 106 | * @param cacheData Cache-specific properties (e.g., texture gen ID) |
| bsalomon@google.com | 1fadb20 | 2011-12-12 16:10:08 +0000 | [diff] [blame] | 107 | * @param srcData Pointer to the pixel values. |
| 108 | * @param rowBytes The number of bytes between rows of the texture. Zero |
| 109 | * implies tightly packed rows. |
| 110 | */ |
| robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 111 | GrTexture* createAndLockTexture(const GrTextureParams* params, |
| 112 | const GrTextureDesc& desc, |
| 113 | const GrCacheData& cacheData, |
| 114 | void* srcData, size_t rowBytes); |
| bsalomon@google.com | 1fadb20 | 2011-12-12 16:10:08 +0000 | [diff] [blame] | 115 | |
| 116 | /** |
| bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 117 | * Search for an entry based on key and dimensions. If found, "lock" it and |
| robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 118 | * return it. The return value will be NULL if not found. |
| bsalomon@google.com | 1fadb20 | 2011-12-12 16:10:08 +0000 | [diff] [blame] | 119 | * Must be balanced with an unlockTexture() call. |
| 120 | * |
| bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 121 | * @param desc Description of the texture properties. |
| robertphillips@google.com | 9c2ea84 | 2012-08-13 17:47:59 +0000 | [diff] [blame] | 122 | * @param cacheData Cache-specific properties (e.g., texture gen ID) |
| bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 123 | * @param params The tex params used to draw a texture may help determine |
| 124 | * the cache entry used. (e.g. different versions may exist |
| 125 | * for different wrap modes on GPUs with limited NPOT |
| 126 | * texture support). NULL implies clamp wrap modes. |
| bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 127 | */ |
| robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 128 | GrTexture* findAndLockTexture(const GrTextureDesc& desc, |
| 129 | const GrCacheData& cacheData, |
| 130 | const GrTextureParams* params); |
| bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 131 | /** |
| 132 | * Determines whether a texture is in the cache. If the texture is found it |
| 133 | * will not be locked or returned. This call does not affect the priority of |
| 134 | * the texture for deletion. |
| 135 | */ |
| robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 136 | bool isTextureInCache(const GrTextureDesc& desc, |
| robertphillips@google.com | 9c2ea84 | 2012-08-13 17:47:59 +0000 | [diff] [blame] | 137 | const GrCacheData& cacheData, |
| bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 138 | const GrTextureParams* params) const; |
| bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 139 | |
| 140 | /** |
| 141 | * Enum that determines how closely a returned scratch texture must match |
| 142 | * a provided GrTextureDesc. |
| 143 | */ |
| 144 | enum ScratchTexMatch { |
| 145 | /** |
| 146 | * Finds a texture that exactly matches the descriptor. |
| 147 | */ |
| 148 | kExact_ScratchTexMatch, |
| 149 | /** |
| 150 | * Finds a texture that approximately matches the descriptor. Will be |
| 151 | * at least as large in width and height as desc specifies. If desc |
| 152 | * specifies that texture is a render target then result will be a |
| 153 | * render target. If desc specifies a render target and doesn't set the |
| 154 | * no stencil flag then result will have a stencil. Format and aa level |
| 155 | * will always match. |
| 156 | */ |
| 157 | kApprox_ScratchTexMatch |
| 158 | }; |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 159 | |
| 160 | /** |
| bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 161 | * Returns a texture matching the desc. It's contents are unknown. Subsequent |
| 162 | * requests with the same descriptor are not guaranteed to return the same |
| 163 | * texture. The same texture is guaranteed not be returned again until it is |
| robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 164 | * unlocked. Call must be balanced with an unlockTexture() call. |
| bsalomon@google.com | a39f404 | 2011-04-26 13:18:16 +0000 | [diff] [blame] | 165 | * |
| 166 | * Textures created by createAndLockTexture() hide the complications of |
| 167 | * tiling non-power-of-two textures on APIs that don't support this (e.g. |
| bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 168 | * unextended GLES2). Tiling a npot texture created by lockScratchTexture on |
| bsalomon@google.com | a39f404 | 2011-04-26 13:18:16 +0000 | [diff] [blame] | 169 | * such an API will create gaps in the tiling pattern. This includes clamp |
| 170 | * mode. (This may be addressed in a future update.) |
| bsalomon@google.com | fea37b5 | 2011-04-25 15:51:06 +0000 | [diff] [blame] | 171 | */ |
| robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 172 | GrTexture* lockScratchTexture(const GrTextureDesc& desc, |
| 173 | ScratchTexMatch match); |
| bsalomon@google.com | b5b3168 | 2011-06-16 18:05:35 +0000 | [diff] [blame] | 174 | |
| 175 | /** |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 176 | * When done with an entry, call unlockTexture(entry) on it, which returns |
| 177 | * it to the cache, where it may be purged. |
| 178 | */ |
| robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 179 | void unlockTexture(GrTexture* texture); |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 180 | |
| 181 | /** |
| robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 182 | * Free any data associated with the provided entry in the texture cache. |
| 183 | * Currently this entry point is only used when a scratch texture is |
| 184 | * detached from the cache. In this case the GrResourceEntry* associated |
| 185 | * with the texture needs to be freed since it will be re-allocated when |
| 186 | * the texture is re-added. This entry point will be removed soon since the |
| 187 | * texture can now carry around a pointer to its GrResourceEntry* (and |
| 188 | * will eventually take over its functionality). |
| robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 189 | */ |
| robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 190 | void freeEntry(GrTexture* texture); |
| robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 191 | |
| 192 | /** |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 193 | * Creates a texture that is outside the cache. Does not count against |
| 194 | * cache's budget. |
| 195 | */ |
| robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 196 | GrTexture* createUncachedTexture(const GrTextureDesc& desc, |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 197 | void* srcData, |
| 198 | size_t rowBytes); |
| 199 | |
| 200 | /** |
| bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 201 | * Returns true if the specified use of an indexed texture is supported. |
| 202 | * Support may depend upon whether the texture params indicate that the |
| 203 | * texture will be tiled. Passing NULL for the texture params indicates |
| 204 | * clamp mode. |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 205 | */ |
| bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 206 | bool supportsIndex8PixelConfig(const GrTextureParams*, |
| bsalomon@google.com | 1f221a7 | 2011-08-23 20:54:07 +0000 | [diff] [blame] | 207 | int width, |
| 208 | int height) const; |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 209 | |
| 210 | /** |
| bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 211 | * Return the current texture cache limits. |
| 212 | * |
| 213 | * @param maxTextures If non-null, returns maximum number of textures that |
| 214 | * can be held in the cache. |
| 215 | * @param maxTextureBytes If non-null, returns maximum number of bytes of |
| 216 | * texture memory that can be held in the cache. |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 217 | */ |
| bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 218 | void getTextureCacheLimits(int* maxTextures, size_t* maxTextureBytes) const; |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 219 | |
| 220 | /** |
| bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 221 | * Specify the texture cache limits. If the current cache exceeds either |
| 222 | * of these, it will be purged (LRU) to keep the cache within these limits. |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 223 | * |
| bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 224 | * @param maxTextures The maximum number of textures that can be held in |
| 225 | * the cache. |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 226 | * @param maxTextureBytes The maximum number of bytes of texture memory |
| 227 | * that can be held in the cache. |
| 228 | */ |
| bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 229 | void setTextureCacheLimits(int maxTextures, size_t maxTextureBytes); |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 230 | |
| 231 | /** |
| 232 | * Return the max width or height of a texture supported by the current gpu |
| 233 | */ |
| bsalomon@google.com | 9195836 | 2011-06-13 17:58:13 +0000 | [diff] [blame] | 234 | int getMaxTextureSize() const; |
| 235 | |
| 236 | /** |
| 237 | * Return the max width or height of a render target supported by the |
| 238 | * current gpu |
| 239 | */ |
| 240 | int getMaxRenderTargetSize() const; |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 241 | |
| 242 | /////////////////////////////////////////////////////////////////////////// |
| 243 | // Render targets |
| 244 | |
| 245 | /** |
| bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 246 | * Sets the render target. |
| 247 | * @param target the render target to set. (should not be NULL.) |
| 248 | */ |
| 249 | void setRenderTarget(GrRenderTarget* target); |
| 250 | |
| 251 | /** |
| 252 | * Gets the current render target. |
| 253 | * @return the currently bound render target. Should never be NULL. |
| 254 | */ |
| 255 | const GrRenderTarget* getRenderTarget() const; |
| 256 | GrRenderTarget* getRenderTarget(); |
| 257 | |
| robertphillips@google.com | f69a11b | 2012-06-15 13:58:07 +0000 | [diff] [blame] | 258 | GrAARectRenderer* getAARectRenderer() { return fAARectRenderer; } |
| 259 | |
| robertphillips@google.com | 99a5ac0 | 2012-04-10 19:26:38 +0000 | [diff] [blame] | 260 | /** |
| 261 | * Can the provided configuration act as a color render target? |
| 262 | */ |
| 263 | bool isConfigRenderable(GrPixelConfig config) const; |
| 264 | |
| bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 265 | /////////////////////////////////////////////////////////////////////////// |
| 266 | // Platform Surfaces |
| 267 | |
| bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 268 | /** |
| bsalomon@google.com | e269f21 | 2011-11-07 13:29:52 +0000 | [diff] [blame] | 269 | * Wraps an existing texture with a GrTexture object. |
| 270 | * |
| 271 | * OpenGL: if the object is a texture Gr may change its GL texture params |
| 272 | * when it is drawn. |
| 273 | * |
| 274 | * @param desc description of the object to create. |
| 275 | * |
| 276 | * @return GrTexture object or NULL on failure. |
| 277 | */ |
| 278 | GrTexture* createPlatformTexture(const GrPlatformTextureDesc& desc); |
| 279 | |
| 280 | /** |
| 281 | * Wraps an existing render target with a GrRenderTarget object. It is |
| 282 | * similar to createPlatformTexture but can be used to draw into surfaces |
| 283 | * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that |
| 284 | * the client will resolve to a texture). |
| 285 | * |
| 286 | * @param desc description of the object to create. |
| 287 | * |
| 288 | * @return GrTexture object or NULL on failure. |
| 289 | */ |
| 290 | GrRenderTarget* createPlatformRenderTarget( |
| 291 | const GrPlatformRenderTargetDesc& desc); |
| 292 | |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 293 | /////////////////////////////////////////////////////////////////////////// |
| 294 | // Matrix state |
| 295 | |
| 296 | /** |
| 297 | * Gets the current transformation matrix. |
| 298 | * @return the current matrix. |
| 299 | */ |
| 300 | const GrMatrix& getMatrix() const; |
| 301 | |
| 302 | /** |
| 303 | * Sets the transformation matrix. |
| 304 | * @param m the matrix to set. |
| 305 | */ |
| 306 | void setMatrix(const GrMatrix& m); |
| 307 | |
| 308 | /** |
| 309 | * Concats the current matrix. The passed matrix is applied before the |
| 310 | * current matrix. |
| 311 | * @param m the matrix to concat. |
| 312 | */ |
| 313 | void concatMatrix(const GrMatrix& m) const; |
| 314 | |
| 315 | |
| 316 | /////////////////////////////////////////////////////////////////////////// |
| 317 | // Clip state |
| 318 | /** |
| 319 | * Gets the current clip. |
| 320 | * @return the current clip. |
| 321 | */ |
| robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 322 | const GrClipData* getClip() const; |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 323 | |
| 324 | /** |
| 325 | * Sets the clip. |
| robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 326 | * @param clipData the clip to set. |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 327 | */ |
| robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 328 | void setClip(const GrClipData* clipData); |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 329 | |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 330 | /////////////////////////////////////////////////////////////////////////// |
| 331 | // Draws |
| 332 | |
| 333 | /** |
| bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 334 | * Clear the entire or rect of the render target, ignoring any clips. |
| 335 | * @param rect the rect to clear or the whole thing if rect is NULL. |
| 336 | * @param color the color to clear to. |
| robertphillips@google.com | c82a8b7 | 2012-06-21 20:15:48 +0000 | [diff] [blame] | 337 | * @param target if non-NULL, the render target to clear otherwise clear |
| 338 | * the current render target |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 339 | */ |
| robertphillips@google.com | c82a8b7 | 2012-06-21 20:15:48 +0000 | [diff] [blame] | 340 | void clear(const GrIRect* rect, GrColor color, |
| 341 | GrRenderTarget* target = NULL); |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 342 | |
| 343 | /** |
| 344 | * Draw everywhere (respecting the clip) with the paint. |
| 345 | */ |
| 346 | void drawPaint(const GrPaint& paint); |
| 347 | |
| 348 | /** |
| 349 | * Draw the rect using a paint. |
| 350 | * @param paint describes how to color pixels. |
| 351 | * @param strokeWidth If strokeWidth < 0, then the rect is filled, else |
| 352 | * the rect is mitered stroked based on strokeWidth. If |
| 353 | * strokeWidth == 0, then the stroke is always a single |
| 354 | * pixel thick. |
| 355 | * @param matrix Optional matrix applied to the rect. Applied before |
| 356 | * context's matrix or the paint's matrix. |
| 357 | * The rects coords are used to access the paint (through texture matrix) |
| 358 | */ |
| 359 | void drawRect(const GrPaint& paint, |
| 360 | const GrRect&, |
| 361 | GrScalar strokeWidth = -1, |
| 362 | const GrMatrix* matrix = NULL); |
| 363 | |
| 364 | /** |
| 365 | * Maps a rect of paint coordinates onto the a rect of destination |
| 366 | * coordinates. Each rect can optionally be transformed. The srcRect |
| 367 | * is stretched over the dstRect. The dstRect is transformed by the |
| 368 | * context's matrix and the srcRect is transformed by the paint's matrix. |
| 369 | * Additional optional matrices can be provided by parameters. |
| 370 | * |
| 371 | * @param paint describes how to color pixels. |
| 372 | * @param dstRect the destination rect to draw. |
| 373 | * @param srcRect rect of paint coordinates to be mapped onto dstRect |
| 374 | * @param dstMatrix Optional matrix to transform dstRect. Applied before |
| 375 | * context's matrix. |
| 376 | * @param srcMatrix Optional matrix to transform srcRect Applied before |
| 377 | * paint's matrix. |
| 378 | */ |
| 379 | void drawRectToRect(const GrPaint& paint, |
| 380 | const GrRect& dstRect, |
| 381 | const GrRect& srcRect, |
| 382 | const GrMatrix* dstMatrix = NULL, |
| 383 | const GrMatrix* srcMatrix = NULL); |
| 384 | |
| 385 | /** |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 386 | * Draws a path. |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 387 | * |
| 388 | * @param paint describes how to color pixels. |
| reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 389 | * @param path the path to draw |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 390 | * @param fill the path filling rule to use. |
| 391 | * @param translate optional additional translation applied to the |
| 392 | * path. |
| 393 | */ |
| bsalomon@google.com | 8d033a1 | 2012-04-27 15:52:53 +0000 | [diff] [blame] | 394 | void drawPath(const GrPaint& paint, const SkPath& path, GrPathFill fill, |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 395 | const GrPoint* translate = NULL); |
| reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 396 | |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 397 | /** |
| 398 | * Draws vertices with a paint. |
| 399 | * |
| 400 | * @param paint describes how to color pixels. |
| 401 | * @param primitiveType primitives type to draw. |
| 402 | * @param vertexCount number of vertices. |
| 403 | * @param positions array of vertex positions, required. |
| 404 | * @param texCoords optional array of texture coordinates used |
| 405 | * to access the paint. |
| 406 | * @param colors optional array of per-vertex colors, supercedes |
| 407 | * the paint's color field. |
| 408 | * @param indices optional array of indices. If NULL vertices |
| 409 | * are drawn non-indexed. |
| 410 | * @param indexCount if indices is non-null then this is the |
| 411 | * number of indices. |
| 412 | */ |
| 413 | void drawVertices(const GrPaint& paint, |
| 414 | GrPrimitiveType primitiveType, |
| 415 | int vertexCount, |
| 416 | const GrPoint positions[], |
| 417 | const GrPoint texs[], |
| 418 | const GrColor colors[], |
| 419 | const uint16_t indices[], |
| 420 | int indexCount); |
| 421 | |
| bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 422 | /** |
| 423 | * Draws an oval. |
| 424 | * |
| 425 | * @param paint describes how to color pixels. |
| 426 | * @param rect the bounding rect of the oval. |
| 427 | * @param strokeWidth if strokeWidth < 0, then the oval is filled, else |
| 428 | * the rect is stroked based on strokeWidth. If |
| 429 | * strokeWidth == 0, then the stroke is always a single |
| 430 | * pixel thick. |
| 431 | */ |
| 432 | void drawOval(const GrPaint& paint, |
| 433 | const GrRect& rect, |
| 434 | SkScalar strokeWidth); |
| 435 | |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 436 | /////////////////////////////////////////////////////////////////////////// |
| 437 | // Misc. |
| 438 | |
| 439 | /** |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 440 | * Flags that affect flush() behavior. |
| 441 | */ |
| 442 | enum FlushBits { |
| 443 | /** |
| 444 | * A client may want Gr to bind a GrRenderTarget in the 3D API so that |
| 445 | * it can be rendered to directly. However, Gr lazily sets state. Simply |
| 446 | * calling setRenderTarget() followed by flush() without flags may not |
| 447 | * bind the render target. This flag forces the context to bind the last |
| 448 | * set render target in the 3D API. |
| 449 | */ |
| 450 | kForceCurrentRenderTarget_FlushBit = 0x1, |
| 451 | /** |
| 452 | * A client may reach a point where it has partially rendered a frame |
| 453 | * through a GrContext that it knows the user will never see. This flag |
| 454 | * causes the flush to skip submission of deferred content to the 3D API |
| 455 | * during the flush. |
| 456 | */ |
| 457 | kDiscard_FlushBit = 0x2, |
| 458 | }; |
| 459 | |
| 460 | /** |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 461 | * Call to ensure all drawing to the context has been issued to the |
| 462 | * underlying 3D API. |
| bsalomon@google.com | a7f84e1 | 2011-03-10 14:13:19 +0000 | [diff] [blame] | 463 | * @param flagsBitfield flags that control the flushing behavior. See |
| 464 | * FlushBits. |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 465 | */ |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 466 | void flush(int flagsBitfield = 0); |
| bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 467 | |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 468 | /** |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 469 | * Reads a rectangle of pixels from a render target. |
| bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 470 | * @param target the render target to read from. NULL means the |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 471 | * current render target. |
| 472 | * @param left left edge of the rectangle to read (inclusive) |
| 473 | * @param top top edge of the rectangle to read (inclusive) |
| 474 | * @param width width of rectangle to read in pixels. |
| 475 | * @param height height of rectangle to read in pixels. |
| 476 | * @param config the pixel config of the destination buffer |
| 477 | * @param buffer memory to read the rectangle into. |
| bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 478 | * @param rowBytes number of bytes bewtween consecutive rows. Zero |
| bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 479 | * means rows are tightly packed. |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 480 | * |
| 481 | * @return true if the read succeeded, false if not. The read can fail |
| bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 482 | * because of an unsupported pixel config or because no render |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 483 | * target is currently set. |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 484 | */ |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 485 | bool readRenderTargetPixels(GrRenderTarget* target, |
| 486 | int left, int top, int width, int height, |
| bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 487 | GrPixelConfig config, void* buffer, |
| bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 488 | size_t rowBytes) { |
| 489 | return this->internalReadRenderTargetPixels(target, left, top, |
| 490 | width, height, |
| 491 | config, buffer, |
| 492 | rowBytes, 0); |
| 493 | } |
| 494 | |
| 495 | /** |
| 496 | * Copy the src pixels [buffer, rowbytes, pixelconfig] into a render target |
| 497 | * at the specified rectangle. |
| 498 | * @param target the render target to write into. NULL means the |
| 499 | * current render target. |
| 500 | * @param left left edge of the rectangle to write (inclusive) |
| 501 | * @param top top edge of the rectangle to write (inclusive) |
| 502 | * @param width width of rectangle to write in pixels. |
| 503 | * @param height height of rectangle to write in pixels. |
| 504 | * @param config the pixel config of the source buffer |
| 505 | * @param buffer memory to read the rectangle from. |
| 506 | * @param rowBytes number of bytes bewtween consecutive rows. Zero |
| 507 | * means rows are tightly packed. |
| 508 | */ |
| 509 | void writeRenderTargetPixels(GrRenderTarget* target, |
| 510 | int left, int top, int width, int height, |
| 511 | GrPixelConfig config, const void* buffer, |
| 512 | size_t rowBytes) { |
| 513 | this->internalWriteRenderTargetPixels(target, left, top, width, height, |
| 514 | config, buffer, rowBytes, 0); |
| 515 | } |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 516 | |
| 517 | /** |
| 518 | * Reads a rectangle of pixels from a texture. |
| bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 519 | * @param texture the texture to read from. |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 520 | * @param left left edge of the rectangle to read (inclusive) |
| 521 | * @param top top edge of the rectangle to read (inclusive) |
| 522 | * @param width width of rectangle to read in pixels. |
| 523 | * @param height height of rectangle to read in pixels. |
| 524 | * @param config the pixel config of the destination buffer |
| 525 | * @param buffer memory to read the rectangle into. |
| bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 526 | * @param rowBytes number of bytes bewtween consecutive rows. Zero |
| 527 | * means rows are tightly packed. |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 528 | * |
| 529 | * @return true if the read succeeded, false if not. The read can fail |
| bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 530 | * because of an unsupported pixel config. |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 531 | */ |
| bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 532 | bool readTexturePixels(GrTexture* texture, |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 533 | int left, int top, int width, int height, |
| bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 534 | GrPixelConfig config, void* buffer, |
| 535 | size_t rowBytes) { |
| 536 | return this->internalReadTexturePixels(texture, left, top, |
| 537 | width, height, |
| 538 | config, buffer, rowBytes, 0); |
| 539 | } |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 540 | |
| 541 | /** |
| bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 542 | * Writes a rectangle of pixels to a texture. |
| 543 | * @param texture the render target to read from. |
| 544 | * @param left left edge of the rectangle to write (inclusive) |
| 545 | * @param top top edge of the rectangle to write (inclusive) |
| 546 | * @param width width of rectangle to write in pixels. |
| 547 | * @param height height of rectangle to write in pixels. |
| 548 | * @param config the pixel config of the source buffer |
| 549 | * @param buffer memory to read pixels from |
| 550 | * @param rowBytes number of bytes bewtween consecutive rows. Zero |
| 551 | * means rows are tightly packed. |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 552 | */ |
| bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 553 | void writeTexturePixels(GrTexture* texture, |
| 554 | int left, int top, int width, int height, |
| 555 | GrPixelConfig config, const void* buffer, |
| 556 | size_t rowBytes) { |
| 557 | this->internalWriteTexturePixels(texture, left, top, width, height, |
| 558 | config, buffer, rowBytes, 0); |
| 559 | } |
| senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 560 | /** |
| senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 561 | * Copies all texels from one texture to another. |
| 562 | * @param src the texture to copy from. |
| 563 | * @param dst the render target to copy to. |
| 564 | */ |
| 565 | void copyTexture(GrTexture* src, GrRenderTarget* dst); |
| bsalomon@google.com | 75f9f25 | 2012-01-31 13:35:56 +0000 | [diff] [blame] | 566 | |
| 567 | /** |
| 568 | * Resolves a render target that has MSAA. The intermediate MSAA buffer is |
| 569 | * downsampled to the associated GrTexture (accessible via |
| 570 | * GrRenderTarget::asTexture()). Any pending draws to the render target will |
| 571 | * be executed before the resolve. |
| 572 | * |
| 573 | * This is only necessary when a client wants to access the object directly |
| 574 | * using the underlying graphics API. GrContext will detect when it must |
| 575 | * perform a resolve to a GrTexture used as the source of a draw or before |
| 576 | * reading pixels back from a GrTexture or GrRenderTarget. |
| 577 | */ |
| 578 | void resolveRenderTarget(GrRenderTarget* target); |
| 579 | |
| senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 580 | /** |
| senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 581 | * Applies a 2D Gaussian blur to a given texture. |
| 582 | * @param srcTexture The source texture to be blurred. |
| senorblanco@chromium.org | 1e95d71 | 2012-07-18 19:52:53 +0000 | [diff] [blame] | 583 | * @param canClobberSrc If true, srcTexture may be overwritten, and |
| 584 | * may be returned as the result. |
| senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 585 | * @param rect The destination rectangle. |
| 586 | * @param sigmaX The blur's standard deviation in X. |
| 587 | * @param sigmaY The blur's standard deviation in Y. |
| senorblanco@chromium.org | 1e95d71 | 2012-07-18 19:52:53 +0000 | [diff] [blame] | 588 | * @return the blurred texture, which may be srcTexture ref'ed, or a |
| 589 | * new texture. It is the caller's responsibility to unref this texture. |
| senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 590 | */ |
| senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 591 | GrTexture* gaussianBlur(GrTexture* srcTexture, |
| senorblanco@chromium.org | 1e95d71 | 2012-07-18 19:52:53 +0000 | [diff] [blame] | 592 | bool canClobberSrc, |
| senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 593 | const SkRect& rect, |
| 594 | float sigmaX, float sigmaY); |
| 595 | |
| senorblanco@chromium.org | aadd9f8 | 2011-07-12 19:44:51 +0000 | [diff] [blame] | 596 | /** |
| bsalomon@google.com | 82aa748 | 2012-08-13 14:22:17 +0000 | [diff] [blame] | 597 | * Zooms a subset of the texture to a larger size with a nice edge. |
| 598 | * The inner rectangle is a simple scaling of the texture by a factor of |
| 599 | * |zoom|. The outer |inset| pixels transition from the background texture |
| 600 | * to the zoomed coordinate system at a rate of |
| 601 | * (distance_to_edge / inset) ^2, producing a rounded lens effect. |
| 602 | * @param srcTexture The source texture to be zoomed. |
| 603 | * @param dstRect The destination rectangle. |
| 604 | * @param srcRect The source rectangle. Must be smaller than |
| 605 | * dstRect |
| 606 | * @param inset Number of pixels to blend along the edges. |
| 607 | * @return the zoomed texture, which is dstTexture. |
| 608 | */ |
| 609 | GrTexture* zoom(GrTexture* srcTexture, |
| 610 | const SkRect& dstRect, const SkRect& srcRect, float inset); |
| 611 | |
| 612 | |
| 613 | /** |
| bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 614 | * This enum is used with the function below, applyMorphology. |
| 615 | */ |
| 616 | enum MorphologyType { |
| 617 | kErode_MorphologyType, |
| 618 | kDilate_MorphologyType, |
| 619 | }; |
| 620 | |
| 621 | /** |
| senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 622 | * Applies a 2D morphology to a given texture. |
| 623 | * @param srcTexture The source texture to be blurred. |
| 624 | * @param rect The destination rectangle. |
| senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 625 | * @param filter The morphology filter. Must be kDilate_Filter or |
| 626 | * kErode_Filter. |
| 627 | * @param radius The morphology radius in X and Y. The filter is |
| 628 | * applied to a fWidth by fHeight rectangle of |
| 629 | * pixels. |
| senorblanco@chromium.org | 1e95d71 | 2012-07-18 19:52:53 +0000 | [diff] [blame] | 630 | * @return the morphed texture, which may be srcTexture ref'ed, or a |
| 631 | * new texture. It is the caller's responsibility to unref this texture. |
| senorblanco@chromium.org | aadd9f8 | 2011-07-12 19:44:51 +0000 | [diff] [blame] | 632 | */ |
| senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 633 | GrTexture* applyMorphology(GrTexture* srcTexture, |
| 634 | const GrRect& rect, |
| bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 635 | MorphologyType type, |
| senorblanco@chromium.org | 3b4dd90 | 2012-03-05 20:41:22 +0000 | [diff] [blame] | 636 | SkISize radius); |
| 637 | |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 638 | /////////////////////////////////////////////////////////////////////////// |
| 639 | // Helpers |
| 640 | |
| 641 | class AutoRenderTarget : ::GrNoncopyable { |
| 642 | public: |
| 643 | AutoRenderTarget(GrContext* context, GrRenderTarget* target) { |
| 644 | fContext = NULL; |
| 645 | fPrevTarget = context->getRenderTarget(); |
| 646 | if (fPrevTarget != target) { |
| 647 | context->setRenderTarget(target); |
| 648 | fContext = context; |
| 649 | } |
| 650 | } |
| 651 | ~AutoRenderTarget() { |
| 652 | if (fContext) { |
| 653 | fContext->setRenderTarget(fPrevTarget); |
| 654 | } |
| 655 | } |
| 656 | private: |
| 657 | GrContext* fContext; |
| 658 | GrRenderTarget* fPrevTarget; |
| 659 | }; |
| 660 | |
| robertphillips@google.com | fea85ac | 2012-07-11 18:53:23 +0000 | [diff] [blame] | 661 | /** |
| 662 | * Save/restore the view-matrix in the context. |
| 663 | */ |
| 664 | class AutoMatrix : GrNoncopyable { |
| 665 | public: |
| 666 | AutoMatrix() : fContext(NULL) {} |
| 667 | AutoMatrix(GrContext* ctx) : fContext(ctx) { |
| 668 | fMatrix = ctx->getMatrix(); |
| 669 | } |
| 670 | AutoMatrix(GrContext* ctx, const GrMatrix& matrix) : fContext(ctx) { |
| 671 | fMatrix = ctx->getMatrix(); |
| 672 | ctx->setMatrix(matrix); |
| 673 | } |
| 674 | void set(GrContext* ctx) { |
| 675 | if (NULL != fContext) { |
| 676 | fContext->setMatrix(fMatrix); |
| 677 | } |
| 678 | fMatrix = ctx->getMatrix(); |
| 679 | fContext = ctx; |
| 680 | } |
| 681 | void set(GrContext* ctx, const GrMatrix& matrix) { |
| 682 | if (NULL != fContext) { |
| 683 | fContext->setMatrix(fMatrix); |
| 684 | } |
| 685 | fMatrix = ctx->getMatrix(); |
| 686 | ctx->setMatrix(matrix); |
| 687 | fContext = ctx; |
| 688 | } |
| 689 | ~AutoMatrix() { |
| 690 | if (NULL != fContext) { |
| 691 | fContext->setMatrix(fMatrix); |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | private: |
| 696 | GrContext* fContext; |
| 697 | GrMatrix fMatrix; |
| 698 | }; |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 699 | |
| robertphillips@google.com | 56c79b1 | 2012-07-11 20:57:46 +0000 | [diff] [blame] | 700 | class AutoClip : GrNoncopyable { |
| 701 | public: |
| 702 | AutoClip(GrContext* context, const GrRect& newClipRect) |
| 703 | : fContext(context) |
| robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 704 | , fNewClipStack(newClipRect) { |
| 705 | fNewClipData.fClipStack = &fNewClipStack; |
| 706 | |
| robertphillips@google.com | 56c79b1 | 2012-07-11 20:57:46 +0000 | [diff] [blame] | 707 | fOldClip = fContext->getClip(); |
| robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 708 | fContext->setClip(&fNewClipData); |
| robertphillips@google.com | 56c79b1 | 2012-07-11 20:57:46 +0000 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | ~AutoClip() { |
| 712 | if (NULL != fContext) { |
| 713 | fContext->setClip(fOldClip); |
| 714 | } |
| 715 | } |
| 716 | private: |
| robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 717 | GrContext* fContext; |
| 718 | const GrClipData* fOldClip; |
| 719 | |
| robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 720 | SkClipStack fNewClipStack; |
| robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 721 | GrClipData fNewClipData; |
| robertphillips@google.com | 56c79b1 | 2012-07-11 20:57:46 +0000 | [diff] [blame] | 722 | }; |
| 723 | |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 724 | /////////////////////////////////////////////////////////////////////////// |
| 725 | // Functions intended for internal use only. |
| 726 | GrGpu* getGpu() { return fGpu; } |
| bsalomon@google.com | 1f221a7 | 2011-08-23 20:54:07 +0000 | [diff] [blame] | 727 | const GrGpu* getGpu() const { return fGpu; } |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 728 | GrFontCache* getFontCache() { return fFontCache; } |
| 729 | GrDrawTarget* getTextTarget(const GrPaint& paint); |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 730 | const GrIndexBuffer* getQuadIndexBuffer() const; |
| bsalomon@google.com | 9923c2b | 2012-06-06 18:21:18 +0000 | [diff] [blame] | 731 | |
| bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 732 | /** |
| 733 | * Stencil buffers add themselves to the cache using |
| 734 | * addAndLockStencilBuffer. When a SB's RT-attachment count |
| 735 | * reaches zero the SB unlocks itself using unlockStencilBuffer and is |
| 736 | * eligible for purging. findStencilBuffer is called to check the cache for |
| 737 | * a SB that matching an RT's criteria. If a match is found that has been |
| 738 | * unlocked (its attachment count has reached 0) then it will be relocked. |
| 739 | */ |
| robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 740 | void addAndLockStencilBuffer(GrStencilBuffer* sb); |
| 741 | void unlockStencilBuffer(GrStencilBuffer* sb); |
| bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 742 | GrStencilBuffer* findStencilBuffer(int width, int height, int sampleCnt); |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 743 | |
| robertphillips@google.com | 2c75681 | 2012-05-22 20:28:23 +0000 | [diff] [blame] | 744 | GrPathRenderer* getPathRenderer(const SkPath& path, |
| 745 | GrPathFill fill, |
| 746 | const GrDrawTarget* target, |
| robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 747 | bool antiAlias, |
| 748 | bool allowSW); |
| robertphillips@google.com | 2c75681 | 2012-05-22 20:28:23 +0000 | [diff] [blame] | 749 | |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 750 | private: |
| bsalomon@google.com | 1d4edd3 | 2012-08-16 18:36:06 +0000 | [diff] [blame] | 751 | // Used to indicate whether a draw should be performed immediately or queued in fDrawBuffer. |
| 752 | enum BufferedDraw { |
| 753 | kYes_BufferedDraw, |
| 754 | kNo_BufferedDraw, |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 755 | }; |
| bsalomon@google.com | 1d4edd3 | 2012-08-16 18:36:06 +0000 | [diff] [blame] | 756 | BufferedDraw fLastDrawWasBuffered; |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 757 | |
| bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 758 | GrGpu* fGpu; |
| bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 759 | GrDrawState* fDrawState; |
| 760 | |
| bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 761 | GrResourceCache* fTextureCache; |
| 762 | GrFontCache* fFontCache; |
| bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 763 | |
| bsalomon@google.com | 3008519 | 2011-08-19 15:42:31 +0000 | [diff] [blame] | 764 | GrPathRendererChain* fPathRendererChain; |
| robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 765 | GrSoftwarePathRenderer* fSoftwarePathRenderer; |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 766 | |
| 767 | GrVertexBufferAllocPool* fDrawBufferVBAllocPool; |
| 768 | GrIndexBufferAllocPool* fDrawBufferIBAllocPool; |
| 769 | GrInOrderDrawBuffer* fDrawBuffer; |
| 770 | |
| robertphillips@google.com | f69a11b | 2012-06-15 13:58:07 +0000 | [diff] [blame] | 771 | GrAARectRenderer* fAARectRenderer; |
| bsalomon@google.com | dfe75bc | 2011-03-25 12:31:16 +0000 | [diff] [blame] | 772 | |
| robertphillips@google.com | f69a11b | 2012-06-15 13:58:07 +0000 | [diff] [blame] | 773 | GrContext(GrGpu* gpu); |
| bsalomon@google.com | 205d460 | 2011-04-25 12:43:45 +0000 | [diff] [blame] | 774 | |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 775 | void setupDrawBuffer(); |
| 776 | |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 777 | void flushDrawBuffer(); |
| 778 | |
| bsalomon@google.com | 10e04bf | 2012-03-30 14:35:04 +0000 | [diff] [blame] | 779 | void setPaint(const GrPaint& paint); |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 780 | |
| bsalomon@google.com | 07ea2db | 2012-08-17 14:06:49 +0000 | [diff] [blame^] | 781 | /// Sets the paint and returns the target to draw into. The paint can be NULL in which case the |
| 782 | /// draw state is left unmodified. |
| 783 | GrDrawTarget* prepareToDraw(const GrPaint*, BufferedDraw); |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 784 | |
| bsalomon@google.com | 8d033a1 | 2012-04-27 15:52:53 +0000 | [diff] [blame] | 785 | void internalDrawPath(const GrPaint& paint, const SkPath& path, |
| bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 786 | GrPathFill fill, const GrPoint* translate); |
| 787 | |
| bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 788 | /** |
| 789 | * Flags to the internal read/write pixels funcs |
| 790 | */ |
| 791 | enum PixelOpsFlags { |
| 792 | kDontFlush_PixelOpsFlag = 0x1, |
| 793 | }; |
| 794 | |
| robertphillips@google.com | 3319f33 | 2012-08-13 18:00:36 +0000 | [diff] [blame] | 795 | GrTexture* createResizedTexture(const GrTextureDesc& desc, |
| 796 | const GrCacheData& cacheData, |
| 797 | void* srcData, |
| 798 | size_t rowBytes, |
| 799 | bool needsFiltering); |
| 800 | |
| bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 801 | bool internalReadRenderTargetPixels(GrRenderTarget* target, |
| 802 | int left, int top, |
| 803 | int width, int height, |
| 804 | GrPixelConfig config, void* buffer, |
| 805 | size_t rowBytes, uint32_t flags); |
| 806 | |
| 807 | void internalWriteRenderTargetPixels(GrRenderTarget* target, |
| 808 | int left, int top, |
| 809 | int width, int height, |
| 810 | GrPixelConfig, const void* buffer, |
| 811 | size_t rowBytes, uint32_t flags); |
| 812 | |
| 813 | bool internalReadTexturePixels(GrTexture* texture, |
| 814 | int left, int top, |
| 815 | int width, int height, |
| 816 | GrPixelConfig config, void* buffer, |
| 817 | size_t rowBytes, uint32_t flags); |
| 818 | |
| 819 | void internalWriteTexturePixels(GrTexture* texture, |
| 820 | int left, int top, |
| 821 | int width, int height, |
| 822 | GrPixelConfig config, const void* buffer, |
| 823 | size_t rowBytes, uint32_t flags); |
| 824 | // needed for access to internalWriteTexturePixels. TODO: make GrContext |
| 825 | // be a facade for an internal class. Then functions that are public on the |
| 826 | // internal class would have only be callable in src/gpu. The facade would |
| 827 | // only have to functions necessary for clients. |
| 828 | friend class GrAtlas; |
| rileya@google.com | 2e2aedc | 2012-08-13 20:28:48 +0000 | [diff] [blame] | 829 | friend class GrTextureStripAtlas; |
| bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 830 | |
| robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 831 | // Needed so GrTexture's returnToCache helper function can call |
| 832 | // addExistingTextureToCache |
| 833 | friend class GrTexture; |
| 834 | |
| 835 | // Add an existing texture to the texture cache. This is intended solely |
| 836 | // for use with textures released from an GrAutoScratchTexture. |
| 837 | void addExistingTextureToCache(GrTexture* texture); |
| reed@google.com | fa35e3d | 2012-06-26 20:16:17 +0000 | [diff] [blame] | 838 | |
| 839 | typedef GrRefCnt INHERITED; |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 840 | }; |
| 841 | |
| 842 | /** |
| bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 843 | * Gets and locks a scratch texture from a descriptor using |
| 844 | * either exact or approximate criteria. Unlocks texture in |
| 845 | * the destructor. |
| senorblanco@chromium.org | aadd9f8 | 2011-07-12 19:44:51 +0000 | [diff] [blame] | 846 | */ |
| bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 847 | class GrAutoScratchTexture : ::GrNoncopyable { |
| senorblanco@chromium.org | aadd9f8 | 2011-07-12 19:44:51 +0000 | [diff] [blame] | 848 | public: |
| bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 849 | GrAutoScratchTexture() |
| robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 850 | : fContext(NULL) |
| 851 | , fTexture(NULL) { |
| senorblanco@chromium.org | aadd9f8 | 2011-07-12 19:44:51 +0000 | [diff] [blame] | 852 | } |
| bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 853 | |
| 854 | GrAutoScratchTexture(GrContext* context, |
| 855 | const GrTextureDesc& desc, |
| 856 | GrContext::ScratchTexMatch match = |
| 857 | GrContext::kApprox_ScratchTexMatch) |
| robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 858 | : fContext(NULL) |
| 859 | , fTexture(NULL) { |
| bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 860 | this->set(context, desc, match); |
| 861 | } |
| 862 | |
| 863 | ~GrAutoScratchTexture() { |
| robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 864 | this->reset(); |
| 865 | } |
| 866 | |
| 867 | void reset() { |
| robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 868 | if (NULL != fContext && NULL != fTexture) { |
| 869 | fContext->unlockTexture(fTexture); |
| 870 | fTexture = NULL; |
| senorblanco@chromium.org | aadd9f8 | 2011-07-12 19:44:51 +0000 | [diff] [blame] | 871 | } |
| 872 | } |
| bsalomon@google.com | 8422311 | 2011-07-14 14:45:44 +0000 | [diff] [blame] | 873 | |
| robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 874 | /* |
| 875 | * When detaching a texture we do not unlock it in the texture cache but |
| 876 | * we do set the returnToCache flag. In this way the texture remains |
| 877 | * "locked" in the texture cache until it is freed and recycled in |
| 878 | * GrTexture::internal_dispose. In reality, the texture has been removed |
| 879 | * from the cache (because this is in AutoScratchTexture) and by not |
| 880 | * calling unlockTexture we simply don't re-add it. It will be reattached |
| 881 | * in GrTexture::internal_dispose. |
| 882 | * |
| 883 | * Note that the caller is assumed to accept and manage the ref to the |
| 884 | * returned texture. |
| 885 | */ |
| 886 | GrTexture* detach() { |
| robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 887 | GrTexture* temp = fTexture; |
| robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 888 | |
| robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 889 | // freeEntry will remove the texture cache's ref |
| 890 | temp->ref(); |
| robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 891 | fContext->freeEntry(fTexture); |
| 892 | fTexture = NULL; |
| robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 893 | |
| 894 | temp->setFlag((GrTextureFlags) GrTexture::kReturnToCache_FlagBit); |
| robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 895 | return temp; |
| 896 | } |
| 897 | |
| bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 898 | GrTexture* set(GrContext* context, |
| 899 | const GrTextureDesc& desc, |
| 900 | GrContext::ScratchTexMatch match = |
| 901 | GrContext::kApprox_ScratchTexMatch) { |
| robertphillips@google.com | 9ec0753 | 2012-06-22 12:01:30 +0000 | [diff] [blame] | 902 | this->reset(); |
| 903 | |
| bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 904 | fContext = context; |
| 905 | if (NULL != fContext) { |
| robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 906 | fTexture = fContext->lockScratchTexture(desc, match); |
| 907 | if (NULL == fTexture) { |
| bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 908 | fContext = NULL; |
| 909 | } |
| robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 910 | return fTexture; |
| bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 911 | } else { |
| 912 | return NULL; |
| 913 | } |
| 914 | } |
| 915 | |
| robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 916 | GrTexture* texture() { return fTexture; } |
| 917 | |
| senorblanco@chromium.org | aadd9f8 | 2011-07-12 19:44:51 +0000 | [diff] [blame] | 918 | private: |
| bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 919 | GrContext* fContext; |
| robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 920 | GrTexture* fTexture; |
| senorblanco@chromium.org | aadd9f8 | 2011-07-12 19:44:51 +0000 | [diff] [blame] | 921 | }; |
| 922 | |
| bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 923 | #endif |