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