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