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