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