| reed@google.com | 873cb1e | 2010-12-23 15:00:45 +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 |
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 17 | #ifndef GrContext_DEFINED
|
| 18 | #define GrContext_DEFINED
|
| 19 |
|
| 20 | #include "GrClip.h"
|
| 21 | #include "GrGpu.h"
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 22 | #include "GrTextureCache.h"
|
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 23 | #include "GrPaint.h"
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 24 |
|
| 25 | class GrFontCache;
|
| 26 | class GrPathIter;
|
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 27 | class GrVertexBufferAllocPool;
|
| 28 | class GrIndexBufferAllocPool;
|
| 29 | class GrInOrderDrawBuffer;
|
| bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 30 | class GrPathRenderer;
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 31 |
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 32 | class GrContext : public GrRefCnt {
|
| 33 | public:
|
| 34 | /**
|
| 35 | * Creates a GrContext from within a 3D context.
|
| 36 | */
|
| 37 | static GrContext* Create(GrGpu::Engine engine,
|
| 38 | GrGpu::Platform3DContext context3D);
|
| 39 |
|
| reed@google.com | 873cb1e | 2010-12-23 15:00:45 +0000 | [diff] [blame] | 40 | /**
|
| 41 | * Helper to create a opengl-shader based context
|
| 42 | */
|
| 43 | static GrContext* CreateGLShaderContext();
|
| bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 44 |
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 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 | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 55 | ///////////////////////////////////////////////////////////////////////////
|
| 56 | // Textures
|
| 57 |
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 58 | /**
|
| 59 | * Abandons all textures. Call this if you have lost the associated GPU
|
| 60 | * context, and thus internal texture references/IDs are now invalid.
|
| 61 | */
|
| 62 | void abandonAllTextures();
|
| 63 |
|
| 64 | /**
|
| 65 | * Search for an entry with the same Key. If found, "lock" it and return it.
|
| 66 | * If not found, return null.
|
| 67 | */
|
| 68 | GrTextureEntry* findAndLockTexture(GrTextureKey*,
|
| 69 | const GrSamplerState&);
|
| 70 |
|
| 71 |
|
| 72 | /**
|
| 73 | * Create a new entry, based on the specified key and texture, and return
|
| 74 | * its "locked" entry.
|
| 75 | *
|
| 76 | * Ownership of the texture is transferred to the Entry, which will unref()
|
| 77 | * it when we are purged or deleted.
|
| 78 | */
|
| 79 | GrTextureEntry* createAndLockTexture(GrTextureKey* key,
|
| 80 | const GrSamplerState&,
|
| 81 | const GrGpu::TextureDesc&,
|
| 82 | void* srcData, size_t rowBytes);
|
| 83 |
|
| 84 | /**
|
| 85 | * When done with an entry, call unlockTexture(entry) on it, which returns
|
| 86 | * it to the cache, where it may be purged.
|
| 87 | */
|
| 88 | void unlockTexture(GrTextureEntry* entry);
|
| 89 |
|
| 90 | /**
|
| 91 | * Removes an texture from the cache. This prevents the texture from
|
| 92 | * being found by a subsequent findAndLockTexture() until it is
|
| 93 | * reattached. The entry still counts against the cache's budget and should
|
| 94 | * be reattached when exclusive access is no longer needed.
|
| 95 | */
|
| 96 | void detachCachedTexture(GrTextureEntry*);
|
| 97 |
|
| 98 | /**
|
| 99 | * Reattaches a texture to the cache and unlocks it. Allows it to be found
|
| 100 | * by a subsequent findAndLock or be purged (provided its lock count is
|
| 101 | * now 0.)
|
| 102 | */
|
| 103 | void reattachAndUnlockCachedTexture(GrTextureEntry*);
|
| 104 |
|
| 105 | /**
|
| 106 | * Creates a texture that is outside the cache. Does not count against
|
| 107 | * cache's budget.
|
| 108 | */
|
| 109 | GrTexture* createUncachedTexture(const GrGpu::TextureDesc&,
|
| 110 | void* srcData,
|
| 111 | size_t rowBytes);
|
| 112 |
|
| 113 | /**
|
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 114 | * Returns true if the specified use of an indexed texture is supported.
|
| 115 | */
|
| 116 | bool supportsIndex8PixelConfig(const GrSamplerState&, int width, int height);
|
| 117 |
|
| 118 | /**
|
| 119 | * Return the current texture cache limits.
|
| 120 | *
|
| 121 | * @param maxTextures If non-null, returns maximum number of textures that
|
| 122 | * can be held in the cache.
|
| 123 | * @param maxTextureBytes If non-null, returns maximum number of bytes of
|
| 124 | * texture memory that can be held in the cache.
|
| 125 | */
|
| 126 | void getTextureCacheLimits(int* maxTextures, size_t* maxTextureBytes) const;
|
| 127 |
|
| 128 | /**
|
| 129 | * Specify the texture cache limits. If the current cache exceeds either
|
| 130 | * of these, it will be purged (LRU) to keep the cache within these limits.
|
| 131 | *
|
| 132 | * @param maxTextures The maximum number of textures that can be held in
|
| 133 | * the cache.
|
| 134 | * @param maxTextureBytes The maximum number of bytes of texture memory
|
| 135 | * that can be held in the cache.
|
| 136 | */
|
| 137 | void setTextureCacheLimits(int maxTextures, size_t maxTextureBytes);
|
| 138 |
|
| reed@google.com | 02a7e6c | 2011-01-28 21:21:49 +0000 | [diff] [blame] | 139 | /**
|
| 140 | * Return the max width or height of a texture supported by the current gpu
|
| 141 | */
|
| 142 | int getMaxTextureDimension();
|
| 143 |
|
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 144 | ///////////////////////////////////////////////////////////////////////////
|
| 145 | // Render targets
|
| 146 |
|
| 147 | /**
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 148 | * Wraps an externally-created rendertarget in a GrRenderTarget.
|
| bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 149 | * @param platformRenderTarget 3D API-specific render target identifier
|
| 150 | * e.g. in GL platforamRenderTarget is an FBO
|
| 151 | * id.
|
| 152 | * @param stencilBits the number of stencil bits that the render
|
| 153 | * target has.
|
| 154 | * @param width width of the render target.
|
| 155 | * @param height height of the render target.
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 156 | */
|
| 157 | GrRenderTarget* createPlatformRenderTarget(intptr_t platformRenderTarget,
|
| bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 158 | int stencilBits,
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 159 | int width, int height);
|
| 160 |
|
| 161 | /**
|
| bsalomon@google.com | 2e7b43d | 2011-01-18 20:57:22 +0000 | [diff] [blame] | 162 | * Reads the current target object (e.g. FBO or IDirect3DSurface9*) and
|
| 163 | * viewport state from the underlying 3D API and wraps it in a
|
| 164 | * GrRenderTarget. The GrRenderTarget will not attempt to delete/destroy the
|
| 165 | * underlying object in its destructor and it is up to caller to guarantee
|
| 166 | * that it remains valid while the GrRenderTarget is used.
|
| 167 | *
|
| 168 | * @return the newly created GrRenderTarget
|
| 169 | */
|
| 170 | GrRenderTarget* createRenderTargetFrom3DApiState() {
|
| 171 | return fGpu->createRenderTargetFrom3DApiState();
|
| 172 | }
|
| 173 |
|
| 174 | /**
|
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 175 | * Sets the render target.
|
| 176 | * @param target the render target to set. (should not be NULL.)
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 177 | */
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 178 | void setRenderTarget(GrRenderTarget* target);
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 179 |
|
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 180 | /**
|
| 181 | * Gets the current render target.
|
| 182 | * @return the currently bound render target. Should never be NULL.
|
| 183 | */
|
| 184 | const GrRenderTarget* getRenderTarget() const;
|
| 185 | GrRenderTarget* getRenderTarget();
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 186 |
|
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 187 | ///////////////////////////////////////////////////////////////////////////
|
| 188 | // Matrix state
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 189 |
|
| 190 | /**
|
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 191 | * Gets the current transformation matrix.
|
| 192 | * @return the current matrix.
|
| 193 | */
|
| 194 | const GrMatrix& getMatrix() const;
|
| 195 |
|
| 196 | /**
|
| 197 | * Sets the transformation matrix.
|
| 198 | * @param m the matrix to set.
|
| 199 | */
|
| 200 | void setMatrix(const GrMatrix& m);
|
| 201 |
|
| 202 | /**
|
| 203 | * Concats the current matrix. The passed matrix is applied before the
|
| 204 | * current matrix.
|
| 205 | * @param m the matrix to concat.
|
| 206 | */
|
| 207 | void concatMatrix(const GrMatrix& m) const;
|
| 208 |
|
| 209 |
|
| 210 | ///////////////////////////////////////////////////////////////////////////
|
| 211 | // Clip state
|
| 212 | /**
|
| 213 | * Gets the current clip.
|
| 214 | * @return the current clip.
|
| 215 | */
|
| 216 | const GrClip& getClip() const { return fGpu->getClip(); }
|
| 217 |
|
| 218 | /**
|
| 219 | * Sets the clip.
|
| 220 | * @param clip the clip to set.
|
| 221 | */
|
| 222 | void setClip(const GrClip& clip);
|
| 223 |
|
| 224 | /**
|
| 225 | * Convenience method for setting the clip to a rect.
|
| 226 | * @param rect the rect to set as the new clip.
|
| 227 | */
|
| 228 | void setClip(const GrIRect& rect);
|
| 229 |
|
| 230 | ///////////////////////////////////////////////////////////////////////////
|
| 231 | // Draws
|
| 232 |
|
| 233 | /**
|
| 234 | * Erase the entire render target, ignoring any clips
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 235 | */
|
| 236 | void eraseColor(GrColor color);
|
| 237 |
|
| 238 | /**
|
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 239 | * Draw everywhere (respecting the clip) with the paint.
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 240 | */
|
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 241 | void drawPaint(const GrPaint& paint);
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 242 |
|
| 243 | /**
|
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 244 | * Draw the rect using a paint.
|
| bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 245 | * @param paint describes how to color pixels.
|
| 246 | * @param strokeWidth If strokeWidth < 0, then the rect is filled, else
|
| 247 | * the rect is mitered stroked based on strokeWidth. If
|
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 248 | * strokeWidth == 0, then the stroke is always a single
|
| bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 249 | * pixel thick.
|
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 250 | * @param matrix Optional matrix applied to the rect. Applied before
|
| bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 251 | * context's matrix or the paint's matrix.
|
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 252 | * The rects coords are used to access the paint (through texture matrix)
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 253 | */
|
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 254 | void drawRect(const GrPaint& paint,
|
| 255 | const GrRect&,
|
| bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 256 | GrScalar strokeWidth = -1,
|
| 257 | const GrMatrix* matrix = NULL);
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 258 |
|
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 259 | /**
|
| 260 | * Maps a rect of paint coordinates onto the a rect of destination
|
| bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 261 | * coordinates. Each rect can optionally be transformed. The srcRect
|
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 262 | * is stretched over the dstRect. The dstRect is transformed by the
|
| bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 263 | * context's matrix and the srcRect is transformed by the paint's matrix.
|
| 264 | * Additional optional matrices can be provided by parameters.
|
| 265 | *
|
| 266 | * @param paint describes how to color pixels.
|
| 267 | * @param dstRect the destination rect to draw.
|
| 268 | * @param srcRect rect of paint coordinates to be mapped onto dstRect
|
| 269 | * @param dstMatrix Optional matrix to transform dstRect. Applied before
|
| 270 | * context's matrix.
|
| 271 | * @param srcMatrix Optional matrix to transform srcRect Applied before
|
| 272 | * paint's matrix.
|
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 273 | */
|
| 274 | void drawRectToRect(const GrPaint& paint,
|
| 275 | const GrRect& dstRect,
|
| bsalomon@google.com | 6f7fbc9 | 2011-02-01 19:12:40 +0000 | [diff] [blame] | 276 | const GrRect& srcRect,
|
| 277 | const GrMatrix* dstMatrix = NULL,
|
| 278 | const GrMatrix* srcMatrix = NULL);
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 279 |
|
| 280 | /**
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 281 | * Tessellates and draws a path.
|
| 282 | *
|
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 283 | * @param paint describes how to color pixels.
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 284 | * @param path the path to draw
|
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 285 | * @param fill the path filling rule to use.
|
| 286 | * @param translate optional additional translation applied to the
|
| 287 | * path.
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 288 | */
|
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 289 | void drawPath(const GrPaint& paint,
|
| 290 | GrPathIter* path,
|
| bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 291 | GrPathFill fill,
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 292 | const GrPoint* translate = NULL);
|
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 293 | /**
|
| 294 | * Draws vertices with a paint.
|
| 295 | *
|
| 296 | * @param paint describes how to color pixels.
|
| 297 | * @param primitiveType primitives type to draw.
|
| 298 | * @param vertexCount number of vertices.
|
| 299 | * @param positions array of vertex positions, required.
|
| 300 | * @param texCoords optional array of texture coordinates used
|
| 301 | * to access the paint.
|
| 302 | * @param colors optional array of per-vertex colors, supercedes
|
| 303 | * the paint's color field.
|
| 304 | * @param indices optional array of indices. If NULL vertices
|
| 305 | * are drawn non-indexed.
|
| 306 | * @param indexCount if indices is non-null then this is the
|
| 307 | * number of indices.
|
| 308 | */
|
| 309 | void drawVertices(const GrPaint& paint,
|
| bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 310 | GrPrimitiveType primitiveType,
|
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 311 | int vertexCount,
|
| 312 | const GrPoint positions[],
|
| 313 | const GrPoint texs[],
|
| 314 | const GrColor colors[],
|
| 315 | const uint16_t indices[],
|
| 316 | int indexCount);
|
| 317 |
|
| 318 | /**
|
| 319 | * Similar to drawVertices but caller provides objects that convert to Gr
|
| 320 | * types. The count of vertices is given by posSrc.
|
| 321 | *
|
| 322 | * @param paint describes how to color pixels.
|
| 323 | * @param primitiveType primitives type to draw.
|
| 324 | * @param posSrc Source of vertex positions. Must implement
|
| 325 | * int count() const;
|
| 326 | * void writeValue(int i, GrPoint* point) const;
|
| 327 | * count returns the total number of vertices and
|
| 328 | * writeValue writes a vertex position to point.
|
| 329 | * @param texSrc optional, pass NULL to not use explicit tex
|
| 330 | * coords. If present provides tex coords with
|
| 331 | * method:
|
| 332 | * void writeValue(int i, GrPoint* point) const;
|
| 333 | * @param texSrc optional, pass NULL to not use per-vertex colors
|
| 334 | * If present provides colors with method:
|
| 335 | * void writeValue(int i, GrColor* point) const;
|
| 336 | * @param indices optional, pass NULL for non-indexed drawing. If
|
| 337 | * present supplies indices for indexed drawing
|
| 338 | * with following methods:
|
| 339 | * int count() const;
|
| 340 | * void writeValue(int i, uint16_t* point) const;
|
| 341 | * count returns the number of indices and
|
| 342 | * writeValue supplies each index.
|
| 343 | */
|
| 344 | template <typename POS_SRC,
|
| 345 | typename TEX_SRC,
|
| 346 | typename COL_SRC,
|
| 347 | typename IDX_SRC>
|
| 348 | void drawCustomVertices(const GrPaint& paint,
|
| bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 349 | GrPrimitiveType primitiveType,
|
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 350 | const POS_SRC& posSrc,
|
| 351 | const TEX_SRC* texCoordSrc,
|
| 352 | const COL_SRC* colorSrc,
|
| 353 | const IDX_SRC* idxSrc);
|
| 354 | /**
|
| 355 | * To avoid the problem of having to create a typename for NULL parameters,
|
| 356 | * these reduced versions of drawCustomVertices are provided.
|
| 357 | */
|
| 358 | template <typename POS_SRC>
|
| 359 | void drawCustomVertices(const GrPaint& paint,
|
| bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 360 | GrPrimitiveType primitiveType,
|
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 361 | const POS_SRC& posSrc);
|
| 362 | template <typename POS_SRC, typename TEX_SRC>
|
| 363 | void drawCustomVertices(const GrPaint& paint,
|
| bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 364 | GrPrimitiveType primitiveType,
|
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 365 | const POS_SRC& posSrc,
|
| 366 | const TEX_SRC* texCoordSrc);
|
| 367 | template <typename POS_SRC, typename TEX_SRC, typename COL_SRC>
|
| 368 | void drawCustomVertices(const GrPaint& paint,
|
| bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 369 | GrPrimitiveType primitiveType,
|
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 370 | const POS_SRC& posSrc,
|
| 371 | const TEX_SRC* texCoordSrc,
|
| 372 | const COL_SRC* colorSrc);
|
| 373 |
|
| 374 |
|
| 375 | ///////////////////////////////////////////////////////////////////////////
|
| 376 | // Misc.
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 377 |
|
| 378 | /**
|
| 379 | * Call to ensure all drawing to the context has been issued to the
|
| 380 | * underlying 3D API.
|
| 381 | * if flushRenderTarget is true then after the call the last
|
| 382 | * rendertarget set will be current in the underlying 3D API, otherwise
|
| 383 | * it may not be. It is useful to set if the caller plans to use the 3D
|
| 384 | * context outside of Ganesh to render into the current RT.
|
| 385 | */
|
| 386 | void flush(bool flushRenderTarget);
|
| 387 |
|
| 388 | /**
|
| 389 | * Return true on success, i.e. if we could copy the specified range of
|
| 390 | * pixels from the current render-target into the buffer, converting into
|
| 391 | * the specified pixel-config.
|
| 392 | */
|
| 393 | bool readPixels(int left, int top, int width, int height,
|
| 394 | GrTexture::PixelConfig, void* buffer);
|
| 395 |
|
| 396 | /**
|
| 397 | * Copy the src pixels [buffer, stride, pixelconfig] into the current
|
| 398 | * render-target at the specified rectangle.
|
| 399 | */
|
| 400 | void writePixels(int left, int top, int width, int height,
|
| 401 | GrTexture::PixelConfig, const void* buffer, size_t stride);
|
| 402 |
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 403 |
|
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 404 | ///////////////////////////////////////////////////////////////////////////
|
| 405 | // Statistics
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 406 |
|
| 407 | void resetStats();
|
| 408 |
|
| 409 | const GrGpu::Stats& getStats() const;
|
| 410 |
|
| 411 | void printStats() const;
|
| 412 |
|
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 413 | ///////////////////////////////////////////////////////////////////////////
|
| 414 | // Helpers
|
| 415 |
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 416 | class AutoRenderTarget : ::GrNoncopyable {
|
| 417 | public:
|
| 418 | AutoRenderTarget(GrContext* context, GrRenderTarget* target) {
|
| 419 | fContext = NULL;
|
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 420 | fPrevTarget = context->getRenderTarget();
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 421 | if (fPrevTarget != target) {
|
| 422 | context->setRenderTarget(target);
|
| 423 | fContext = context;
|
| 424 | }
|
| 425 | }
|
| 426 | ~AutoRenderTarget() {
|
| 427 | if (fContext) {
|
| 428 | fContext->setRenderTarget(fPrevTarget);
|
| 429 | }
|
| 430 | }
|
| 431 | private:
|
| 432 | GrContext* fContext;
|
| 433 | GrRenderTarget* fPrevTarget;
|
| 434 | };
|
| 435 |
|
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 436 |
|
| reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 437 | ///////////////////////////////////////////////////////////////////////////
|
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 438 | // Functions intended for internal use only.
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 439 | GrGpu* getGpu() { return fGpu; }
|
| 440 | GrFontCache* getFontCache() { return fFontCache; }
|
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 441 | GrDrawTarget* getTextTarget(const GrPaint& paint);
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 442 | void flushText();
|
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 443 | const GrIndexBuffer* getQuadIndexBuffer() const;
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 444 |
|
| 445 | private:
|
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 446 | // used to keep track of when we need to flush the draw buffer
|
| 447 | enum DrawCategory {
|
| 448 | kBuffered_DrawCategory, // last draw was inserted in draw buffer
|
| 449 | kUnbuffered_DrawCategory, // last draw was not inserted in the draw buffer
|
| 450 | kText_DrawCategory // text context was last to draw
|
| 451 | };
|
| 452 | DrawCategory fLastDrawCategory;
|
| 453 |
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 454 | GrGpu* fGpu;
|
| 455 | GrTextureCache* fTextureCache;
|
| 456 | GrFontCache* fFontCache;
|
| bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 457 | GrPathRenderer* fPathRenderer;
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 458 |
|
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 459 | GrVertexBufferAllocPool* fDrawBufferVBAllocPool;
|
| 460 | GrIndexBufferAllocPool* fDrawBufferIBAllocPool;
|
| 461 | GrInOrderDrawBuffer* fDrawBuffer;
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 462 |
|
| 463 | GrContext(GrGpu* gpu);
|
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 464 | void flushDrawBuffer();
|
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 465 |
|
| 466 | static void SetPaint(const GrPaint& paint, GrDrawTarget* target);
|
| 467 |
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 468 | bool finalizeTextureKey(GrTextureKey*, const GrSamplerState&) const;
|
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 469 |
|
| 470 | GrDrawTarget* prepareToDraw(const GrPaint& paint, DrawCategory drawType);
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 471 |
|
| 472 | void drawClipIntoStencil();
|
| 473 | };
|
| 474 |
|
| 475 | /**
|
| 476 | * Save/restore the view-matrix in the context.
|
| 477 | */
|
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 478 | class GrAutoMatrix : GrNoncopyable {
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 479 | public:
|
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 480 | GrAutoMatrix(GrContext* ctx) : fContext(ctx) {
|
| 481 | fMatrix = ctx->getMatrix();
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 482 | }
|
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 483 | GrAutoMatrix(GrContext* ctx, const GrMatrix& matrix) : fContext(ctx) {
|
| 484 | fMatrix = ctx->getMatrix();
|
| 485 | ctx->setMatrix(matrix);
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 486 | }
|
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 487 | ~GrAutoMatrix() {
|
| 488 | fContext->setMatrix(fMatrix);
|
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 489 | }
|
| 490 |
|
| 491 | private:
|
| 492 | GrContext* fContext;
|
| 493 | GrMatrix fMatrix;
|
| 494 | };
|
| 495 |
|
| 496 | #endif
|
| reed@google.com | 873cb1e | 2010-12-23 15:00:45 +0000 | [diff] [blame] | 497 |
|
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 498 | #include "GrContext_impl.h"
|