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