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