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