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