2011-02-22 23:59:41 +03:00
|
|
|
|
2011-07-28 18:26:00 +04:00
|
|
|
/*
|
|
|
|
* Copyright 2010 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2011-02-22 23:59:41 +03:00
|
|
|
*/
|
|
|
|
|
2011-07-28 18:26:00 +04:00
|
|
|
|
2011-02-22 23:59:41 +03:00
|
|
|
#ifndef GrContext_DEFINED
|
|
|
|
#define GrContext_DEFINED
|
|
|
|
|
|
|
|
#include "GrClip.h"
|
|
|
|
#include "GrPaint.h"
|
2012-06-12 04:32:28 +04:00
|
|
|
#include "GrAARectRenderer.h"
|
2011-08-19 18:49:36 +04:00
|
|
|
// not strictly needed but requires WK change in LayerTextureUpdaterCanvas to
|
|
|
|
// remove.
|
|
|
|
#include "GrRenderTarget.h"
|
2011-02-22 23:59:41 +03:00
|
|
|
|
2012-03-06 00:41:22 +04:00
|
|
|
class GrAutoScratchTexture;
|
2012-03-30 18:35:04 +04:00
|
|
|
class GrDrawState;
|
2011-08-17 17:42:46 +04:00
|
|
|
class GrDrawTarget;
|
2011-02-22 23:59:41 +03:00
|
|
|
class GrFontCache;
|
2011-05-03 01:14:59 +04:00
|
|
|
class GrGpu;
|
2011-08-17 17:42:46 +04:00
|
|
|
class GrIndexBuffer;
|
2011-02-22 23:59:41 +03:00
|
|
|
class GrIndexBufferAllocPool;
|
|
|
|
class GrInOrderDrawBuffer;
|
2011-08-17 17:42:46 +04:00
|
|
|
class GrPathRenderer;
|
2011-08-19 19:42:31 +04:00
|
|
|
class GrPathRendererChain;
|
2011-07-27 00:45:30 +04:00
|
|
|
class GrResourceEntry;
|
|
|
|
class GrResourceCache;
|
2011-08-08 21:01:14 +04:00
|
|
|
class GrStencilBuffer;
|
2011-08-17 17:42:46 +04:00
|
|
|
class GrVertexBuffer;
|
2011-07-27 00:45:30 +04:00
|
|
|
class GrVertexBufferAllocPool;
|
2012-05-23 17:19:12 +04:00
|
|
|
class GrSoftwarePathRenderer;
|
2011-07-27 00:45:30 +04:00
|
|
|
|
2011-03-21 22:51:57 +03:00
|
|
|
class GR_API GrContext : public GrRefCnt {
|
2011-02-22 23:59:41 +03:00
|
|
|
public:
|
2012-06-27 00:16:17 +04:00
|
|
|
SK_DECLARE_INST_COUNT(GrContext)
|
|
|
|
|
2011-02-22 23:59:41 +03:00
|
|
|
/**
|
|
|
|
* Creates a GrContext from within a 3D context.
|
|
|
|
*/
|
2011-05-03 01:14:59 +04:00
|
|
|
static GrContext* Create(GrEngine engine,
|
|
|
|
GrPlatform3DContext context3D);
|
2011-02-22 23:59:41 +03:00
|
|
|
|
2012-06-15 18:10:09 +04:00
|
|
|
/**
|
|
|
|
* Returns the number of GrContext instances for the current thread.
|
|
|
|
*/
|
|
|
|
static int GetThreadInstanceCount();
|
|
|
|
|
2011-02-22 23:59:41 +03:00
|
|
|
virtual ~GrContext();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The GrContext normally assumes that no outsider is setting state
|
|
|
|
* within the underlying 3D API's context/device/whatever. This call informs
|
|
|
|
* the context that the state was modified and it should resend. Shouldn't
|
|
|
|
* be called frequently for good performance.
|
|
|
|
*/
|
|
|
|
void resetContext();
|
|
|
|
|
2011-03-31 01:26:44 +04:00
|
|
|
/**
|
|
|
|
* Abandons all gpu resources, assumes 3D API state is unknown. Call this
|
|
|
|
* if you have lost the associated GPU context, and thus internal texture,
|
|
|
|
* buffer, etc. references/IDs are now invalid. Should be called even when
|
|
|
|
* GrContext is no longer going to be used for two reasons:
|
|
|
|
* 1) ~GrContext will not try to free the objects in the 3D API.
|
|
|
|
* 2) If you've created GrResources that outlive the GrContext they will
|
|
|
|
* be marked as invalid (GrResource::isValid()) and won't attempt to
|
|
|
|
* free their underlying resource in the 3D API.
|
|
|
|
* Content drawn since the last GrContext::flush() may be lost.
|
|
|
|
*/
|
|
|
|
void contextLost();
|
2011-02-22 23:59:41 +03:00
|
|
|
|
2011-06-09 02:55:10 +04:00
|
|
|
/**
|
|
|
|
* Similar to contextLost, but makes no attempt to reset state.
|
|
|
|
* Use this method when GrContext destruction is pending, but
|
|
|
|
* the graphics context is destroyed first.
|
|
|
|
*/
|
|
|
|
void contextDestroyed();
|
|
|
|
|
2011-02-22 23:59:41 +03:00
|
|
|
/**
|
2011-03-31 01:26:44 +04:00
|
|
|
* Frees gpu created by the context. Can be called to reduce GPU memory
|
|
|
|
* pressure.
|
2011-02-22 23:59:41 +03:00
|
|
|
*/
|
2011-03-31 01:26:44 +04:00
|
|
|
void freeGpuResources();
|
|
|
|
|
2012-06-22 19:15:59 +04:00
|
|
|
/**
|
|
|
|
* Returns the number of bytes of GPU memory hosted by the texture cache.
|
|
|
|
*/
|
|
|
|
size_t getGpuTextureCacheBytes() const;
|
|
|
|
|
2011-03-31 01:26:44 +04:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Textures
|
2011-02-22 23:59:41 +03:00
|
|
|
|
|
|
|
/**
|
2011-07-27 00:45:30 +04:00
|
|
|
* Token that refers to an entry in the texture cache. Returned by
|
|
|
|
* functions that lock textures. Passed to unlockTexture.
|
|
|
|
*/
|
2012-02-09 20:07:08 +04:00
|
|
|
class SK_API TextureCacheEntry {
|
2011-07-27 00:45:30 +04:00
|
|
|
public:
|
|
|
|
TextureCacheEntry() : fEntry(NULL) {}
|
|
|
|
TextureCacheEntry(const TextureCacheEntry& e) : fEntry(e.fEntry) {}
|
|
|
|
TextureCacheEntry& operator= (const TextureCacheEntry& e) {
|
|
|
|
fEntry = e.fEntry;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
GrTexture* texture() const;
|
|
|
|
void reset() { fEntry = NULL; }
|
2012-06-22 16:41:43 +04:00
|
|
|
GrResourceEntry* cacheEntry() { return fEntry; }
|
2011-07-27 00:45:30 +04:00
|
|
|
private:
|
|
|
|
explicit TextureCacheEntry(GrResourceEntry* entry) { fEntry = entry; }
|
|
|
|
void set(GrResourceEntry* entry) { fEntry = entry; }
|
|
|
|
GrResourceEntry* fEntry;
|
|
|
|
friend class GrContext;
|
|
|
|
};
|
|
|
|
|
2011-12-12 20:10:08 +04:00
|
|
|
/**
|
|
|
|
* Create a new entry, based on the specified key and texture, and return
|
|
|
|
* its "locked" entry. Must call be balanced with an unlockTexture() call.
|
|
|
|
*
|
2012-07-26 01:27:09 +04:00
|
|
|
* @param params The tex params used to draw a texture may help determine
|
|
|
|
* the cache entry used. (e.g. different versions may exist
|
|
|
|
* for different wrap modes on GPUs with limited NPOT
|
|
|
|
* texture support). NULL implies clamp wrap modes.
|
2011-12-12 20:10:08 +04:00
|
|
|
* @param desc Description of the texture properties.
|
|
|
|
* @param srcData Pointer to the pixel values.
|
|
|
|
* @param rowBytes The number of bytes between rows of the texture. Zero
|
|
|
|
* implies tightly packed rows.
|
|
|
|
*/
|
2012-07-26 01:27:09 +04:00
|
|
|
TextureCacheEntry createAndLockTexture(const GrTextureParams* params,
|
2011-12-12 20:10:08 +04:00
|
|
|
const GrTextureDesc& desc,
|
|
|
|
void* srcData, size_t rowBytes);
|
|
|
|
|
2011-07-27 00:45:30 +04:00
|
|
|
/**
|
|
|
|
* Search for an entry based on key and dimensions. If found, "lock" it and
|
|
|
|
* return it. The entry's texture() function will return NULL if not found.
|
2011-12-12 20:10:08 +04:00
|
|
|
* Must be balanced with an unlockTexture() call.
|
|
|
|
*
|
2012-07-26 01:27:09 +04:00
|
|
|
* @param desc Description of the texture properties.
|
|
|
|
* @param params The tex params used to draw a texture may help determine
|
|
|
|
* the cache entry used. (e.g. different versions may exist
|
|
|
|
* for different wrap modes on GPUs with limited NPOT
|
|
|
|
* texture support). NULL implies clamp wrap modes.
|
2011-07-27 00:45:30 +04:00
|
|
|
*/
|
2012-06-07 16:08:45 +04:00
|
|
|
TextureCacheEntry findAndLockTexture(const GrTextureDesc& desc,
|
2012-07-26 01:27:09 +04:00
|
|
|
const GrTextureParams* params);
|
2011-11-30 18:13:48 +04:00
|
|
|
/**
|
|
|
|
* Determines whether a texture is in the cache. If the texture is found it
|
|
|
|
* will not be locked or returned. This call does not affect the priority of
|
|
|
|
* the texture for deletion.
|
|
|
|
*/
|
2012-06-07 16:08:45 +04:00
|
|
|
bool isTextureInCache(const GrTextureDesc& desc,
|
2012-07-26 01:27:09 +04:00
|
|
|
const GrTextureParams* params) const;
|
2011-07-27 00:45:30 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enum that determines how closely a returned scratch texture must match
|
|
|
|
* a provided GrTextureDesc.
|
|
|
|
*/
|
|
|
|
enum ScratchTexMatch {
|
|
|
|
/**
|
|
|
|
* Finds a texture that exactly matches the descriptor.
|
|
|
|
*/
|
|
|
|
kExact_ScratchTexMatch,
|
|
|
|
/**
|
|
|
|
* Finds a texture that approximately matches the descriptor. Will be
|
|
|
|
* at least as large in width and height as desc specifies. If desc
|
|
|
|
* specifies that texture is a render target then result will be a
|
|
|
|
* render target. If desc specifies a render target and doesn't set the
|
|
|
|
* no stencil flag then result will have a stencil. Format and aa level
|
|
|
|
* will always match.
|
|
|
|
*/
|
|
|
|
kApprox_ScratchTexMatch
|
|
|
|
};
|
2011-02-22 23:59:41 +03:00
|
|
|
|
|
|
|
/**
|
2011-04-25 19:51:06 +04:00
|
|
|
* Returns a texture matching the desc. It's contents are unknown. Subsequent
|
|
|
|
* requests with the same descriptor are not guaranteed to return the same
|
|
|
|
* texture. The same texture is guaranteed not be returned again until it is
|
2012-06-22 16:41:43 +04:00
|
|
|
* unlocked. Call must be balanced with an unlockTexture() call.
|
2011-04-26 17:18:16 +04:00
|
|
|
*
|
|
|
|
* Textures created by createAndLockTexture() hide the complications of
|
|
|
|
* tiling non-power-of-two textures on APIs that don't support this (e.g.
|
2011-07-27 00:45:30 +04:00
|
|
|
* unextended GLES2). Tiling a npot texture created by lockScratchTexture on
|
2011-04-26 17:18:16 +04:00
|
|
|
* such an API will create gaps in the tiling pattern. This includes clamp
|
|
|
|
* mode. (This may be addressed in a future update.)
|
2011-02-22 23:59:41 +03:00
|
|
|
*/
|
2012-06-07 16:08:45 +04:00
|
|
|
TextureCacheEntry lockScratchTexture(const GrTextureDesc& desc,
|
|
|
|
ScratchTexMatch match);
|
2011-06-16 22:05:35 +04:00
|
|
|
|
2011-02-22 23:59:41 +03:00
|
|
|
/**
|
2011-04-25 19:51:06 +04:00
|
|
|
* When done with an entry, call unlockTexture(entry) on it, which returns
|
|
|
|
* it to the cache, where it may be purged.
|
2011-02-22 23:59:41 +03:00
|
|
|
*/
|
2011-07-27 00:45:30 +04:00
|
|
|
void unlockTexture(TextureCacheEntry entry);
|
2011-02-22 23:59:41 +03:00
|
|
|
|
2012-06-22 16:41:43 +04:00
|
|
|
/**
|
|
|
|
* Free any data associated with the provided entry in the texture cache
|
|
|
|
*/
|
|
|
|
void freeEntry(TextureCacheEntry entry);
|
|
|
|
|
2011-02-22 23:59:41 +03:00
|
|
|
/**
|
|
|
|
* Creates a texture that is outside the cache. Does not count against
|
|
|
|
* cache's budget.
|
|
|
|
*/
|
2012-06-07 16:08:45 +04:00
|
|
|
GrTexture* createUncachedTexture(const GrTextureDesc& desc,
|
2011-02-22 23:59:41 +03:00
|
|
|
void* srcData,
|
|
|
|
size_t rowBytes);
|
|
|
|
|
|
|
|
/**
|
2012-07-26 01:27:09 +04:00
|
|
|
* Returns true if the specified use of an indexed texture is supported.
|
|
|
|
* Support may depend upon whether the texture params indicate that the
|
|
|
|
* texture will be tiled. Passing NULL for the texture params indicates
|
|
|
|
* clamp mode.
|
2011-02-22 23:59:41 +03:00
|
|
|
*/
|
2012-07-26 01:27:09 +04:00
|
|
|
bool supportsIndex8PixelConfig(const GrTextureParams*,
|
2011-08-24 00:54:07 +04:00
|
|
|
int width,
|
|
|
|
int height) const;
|
2011-02-22 23:59:41 +03:00
|
|
|
|
|
|
|
/**
|
2012-06-22 19:15:59 +04:00
|
|
|
* Return the current texture cache limits.
|
|
|
|
*
|
|
|
|
* @param maxTextures If non-null, returns maximum number of textures that
|
|
|
|
* can be held in the cache.
|
|
|
|
* @param maxTextureBytes If non-null, returns maximum number of bytes of
|
|
|
|
* texture memory that can be held in the cache.
|
2011-02-22 23:59:41 +03:00
|
|
|
*/
|
2012-06-22 19:15:59 +04:00
|
|
|
void getTextureCacheLimits(int* maxTextures, size_t* maxTextureBytes) const;
|
2011-02-22 23:59:41 +03:00
|
|
|
|
|
|
|
/**
|
2012-06-22 19:15:59 +04:00
|
|
|
* Specify the texture cache limits. If the current cache exceeds either
|
|
|
|
* of these, it will be purged (LRU) to keep the cache within these limits.
|
2011-02-22 23:59:41 +03:00
|
|
|
*
|
2012-06-22 19:15:59 +04:00
|
|
|
* @param maxTextures The maximum number of textures that can be held in
|
|
|
|
* the cache.
|
2011-02-22 23:59:41 +03:00
|
|
|
* @param maxTextureBytes The maximum number of bytes of texture memory
|
|
|
|
* that can be held in the cache.
|
|
|
|
*/
|
2012-06-22 19:15:59 +04:00
|
|
|
void setTextureCacheLimits(int maxTextures, size_t maxTextureBytes);
|
2011-02-22 23:59:41 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the max width or height of a texture supported by the current gpu
|
|
|
|
*/
|
2011-06-13 21:58:13 +04:00
|
|
|
int getMaxTextureSize() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the max width or height of a render target supported by the
|
|
|
|
* current gpu
|
|
|
|
*/
|
|
|
|
int getMaxRenderTargetSize() const;
|
2011-02-22 23:59:41 +03:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Render targets
|
|
|
|
|
|
|
|
/**
|
2011-04-11 21:58:48 +04:00
|
|
|
* Sets the render target.
|
|
|
|
* @param target the render target to set. (should not be NULL.)
|
|
|
|
*/
|
|
|
|
void setRenderTarget(GrRenderTarget* target);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the current render target.
|
|
|
|
* @return the currently bound render target. Should never be NULL.
|
|
|
|
*/
|
|
|
|
const GrRenderTarget* getRenderTarget() const;
|
|
|
|
GrRenderTarget* getRenderTarget();
|
|
|
|
|
2012-06-15 17:58:07 +04:00
|
|
|
GrAARectRenderer* getAARectRenderer() { return fAARectRenderer; }
|
|
|
|
|
2012-04-10 23:26:38 +04:00
|
|
|
/**
|
|
|
|
* Can the provided configuration act as a color render target?
|
|
|
|
*/
|
|
|
|
bool isConfigRenderable(GrPixelConfig config) const;
|
|
|
|
|
2011-04-11 21:58:48 +04:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Platform Surfaces
|
|
|
|
|
|
|
|
/**
|
2011-11-07 17:29:52 +04:00
|
|
|
* Wraps an existing texture with a GrTexture object.
|
|
|
|
*
|
|
|
|
* OpenGL: if the object is a texture Gr may change its GL texture params
|
|
|
|
* when it is drawn.
|
|
|
|
*
|
|
|
|
* @param desc description of the object to create.
|
|
|
|
*
|
|
|
|
* @return GrTexture object or NULL on failure.
|
|
|
|
*/
|
|
|
|
GrTexture* createPlatformTexture(const GrPlatformTextureDesc& desc);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wraps an existing render target with a GrRenderTarget object. It is
|
|
|
|
* similar to createPlatformTexture but can be used to draw into surfaces
|
|
|
|
* that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that
|
|
|
|
* the client will resolve to a texture).
|
|
|
|
*
|
|
|
|
* @param desc description of the object to create.
|
|
|
|
*
|
|
|
|
* @return GrTexture object or NULL on failure.
|
|
|
|
*/
|
|
|
|
GrRenderTarget* createPlatformRenderTarget(
|
|
|
|
const GrPlatformRenderTargetDesc& desc);
|
|
|
|
|
2011-02-22 23:59:41 +03:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Matrix state
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the current transformation matrix.
|
|
|
|
* @return the current matrix.
|
|
|
|
*/
|
|
|
|
const GrMatrix& getMatrix() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the transformation matrix.
|
|
|
|
* @param m the matrix to set.
|
|
|
|
*/
|
|
|
|
void setMatrix(const GrMatrix& m);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Concats the current matrix. The passed matrix is applied before the
|
|
|
|
* current matrix.
|
|
|
|
* @param m the matrix to concat.
|
|
|
|
*/
|
|
|
|
void concatMatrix(const GrMatrix& m) const;
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Clip state
|
|
|
|
/**
|
|
|
|
* Gets the current clip.
|
|
|
|
* @return the current clip.
|
|
|
|
*/
|
2012-07-26 22:52:16 +04:00
|
|
|
const GrClipData* getClip() const;
|
2011-02-22 23:59:41 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the clip.
|
2012-07-26 22:52:16 +04:00
|
|
|
* @param clipData the clip to set.
|
2011-02-22 23:59:41 +03:00
|
|
|
*/
|
2012-07-26 22:52:16 +04:00
|
|
|
void setClip(const GrClipData* clipData);
|
2011-02-22 23:59:41 +03:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Draws
|
|
|
|
|
|
|
|
/**
|
2011-04-27 23:55:29 +04:00
|
|
|
* Clear the entire or rect of the render target, ignoring any clips.
|
|
|
|
* @param rect the rect to clear or the whole thing if rect is NULL.
|
|
|
|
* @param color the color to clear to.
|
2012-06-22 00:15:48 +04:00
|
|
|
* @param target if non-NULL, the render target to clear otherwise clear
|
|
|
|
* the current render target
|
2011-02-22 23:59:41 +03:00
|
|
|
*/
|
2012-06-22 00:15:48 +04:00
|
|
|
void clear(const GrIRect* rect, GrColor color,
|
|
|
|
GrRenderTarget* target = NULL);
|
2011-02-22 23:59:41 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Draw everywhere (respecting the clip) with the paint.
|
|
|
|
*/
|
|
|
|
void drawPaint(const GrPaint& paint);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draw the rect using a paint.
|
|
|
|
* @param paint describes how to color pixels.
|
|
|
|
* @param strokeWidth If strokeWidth < 0, then the rect is filled, else
|
|
|
|
* the rect is mitered stroked based on strokeWidth. If
|
|
|
|
* strokeWidth == 0, then the stroke is always a single
|
|
|
|
* pixel thick.
|
|
|
|
* @param matrix Optional matrix applied to the rect. Applied before
|
|
|
|
* context's matrix or the paint's matrix.
|
|
|
|
* The rects coords are used to access the paint (through texture matrix)
|
|
|
|
*/
|
|
|
|
void drawRect(const GrPaint& paint,
|
|
|
|
const GrRect&,
|
|
|
|
GrScalar strokeWidth = -1,
|
|
|
|
const GrMatrix* matrix = NULL);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maps a rect of paint coordinates onto the a rect of destination
|
|
|
|
* coordinates. Each rect can optionally be transformed. The srcRect
|
|
|
|
* is stretched over the dstRect. The dstRect is transformed by the
|
|
|
|
* context's matrix and the srcRect is transformed by the paint's matrix.
|
|
|
|
* Additional optional matrices can be provided by parameters.
|
|
|
|
*
|
|
|
|
* @param paint describes how to color pixels.
|
|
|
|
* @param dstRect the destination rect to draw.
|
|
|
|
* @param srcRect rect of paint coordinates to be mapped onto dstRect
|
|
|
|
* @param dstMatrix Optional matrix to transform dstRect. Applied before
|
|
|
|
* context's matrix.
|
|
|
|
* @param srcMatrix Optional matrix to transform srcRect Applied before
|
|
|
|
* paint's matrix.
|
|
|
|
*/
|
|
|
|
void drawRectToRect(const GrPaint& paint,
|
|
|
|
const GrRect& dstRect,
|
|
|
|
const GrRect& srcRect,
|
|
|
|
const GrMatrix* dstMatrix = NULL,
|
|
|
|
const GrMatrix* srcMatrix = NULL);
|
|
|
|
|
|
|
|
/**
|
2011-03-03 16:54:13 +03:00
|
|
|
* Draws a path.
|
2011-02-22 23:59:41 +03:00
|
|
|
*
|
|
|
|
* @param paint describes how to color pixels.
|
2011-05-16 21:21:57 +04:00
|
|
|
* @param path the path to draw
|
2011-02-22 23:59:41 +03:00
|
|
|
* @param fill the path filling rule to use.
|
|
|
|
* @param translate optional additional translation applied to the
|
|
|
|
* path.
|
|
|
|
*/
|
2012-04-27 19:52:53 +04:00
|
|
|
void drawPath(const GrPaint& paint, const SkPath& path, GrPathFill fill,
|
2011-02-22 23:59:41 +03:00
|
|
|
const GrPoint* translate = NULL);
|
2011-05-16 21:21:57 +04:00
|
|
|
|
2011-02-22 23:59:41 +03:00
|
|
|
/**
|
|
|
|
* Draws vertices with a paint.
|
|
|
|
*
|
|
|
|
* @param paint describes how to color pixels.
|
|
|
|
* @param primitiveType primitives type to draw.
|
|
|
|
* @param vertexCount number of vertices.
|
|
|
|
* @param positions array of vertex positions, required.
|
|
|
|
* @param texCoords optional array of texture coordinates used
|
|
|
|
* to access the paint.
|
|
|
|
* @param colors optional array of per-vertex colors, supercedes
|
|
|
|
* the paint's color field.
|
|
|
|
* @param indices optional array of indices. If NULL vertices
|
|
|
|
* are drawn non-indexed.
|
|
|
|
* @param indexCount if indices is non-null then this is the
|
|
|
|
* number of indices.
|
|
|
|
*/
|
|
|
|
void drawVertices(const GrPaint& paint,
|
|
|
|
GrPrimitiveType primitiveType,
|
|
|
|
int vertexCount,
|
|
|
|
const GrPoint positions[],
|
|
|
|
const GrPoint texs[],
|
|
|
|
const GrColor colors[],
|
|
|
|
const uint16_t indices[],
|
|
|
|
int indexCount);
|
|
|
|
|
2012-04-27 17:05:21 +04:00
|
|
|
/**
|
|
|
|
* Draws an oval.
|
|
|
|
*
|
|
|
|
* @param paint describes how to color pixels.
|
|
|
|
* @param rect the bounding rect of the oval.
|
|
|
|
* @param strokeWidth if strokeWidth < 0, then the oval is filled, else
|
|
|
|
* the rect is stroked based on strokeWidth. If
|
|
|
|
* strokeWidth == 0, then the stroke is always a single
|
|
|
|
* pixel thick.
|
|
|
|
*/
|
|
|
|
void drawOval(const GrPaint& paint,
|
|
|
|
const GrRect& rect,
|
|
|
|
SkScalar strokeWidth);
|
|
|
|
|
2011-02-22 23:59:41 +03:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Misc.
|
|
|
|
|
2011-03-10 17:13:19 +03:00
|
|
|
/**
|
|
|
|
* Flags that affect flush() behavior.
|
|
|
|
*/
|
|
|
|
enum FlushBits {
|
|
|
|
/**
|
|
|
|
* A client may want Gr to bind a GrRenderTarget in the 3D API so that
|
|
|
|
* it can be rendered to directly. However, Gr lazily sets state. Simply
|
|
|
|
* calling setRenderTarget() followed by flush() without flags may not
|
|
|
|
* bind the render target. This flag forces the context to bind the last
|
|
|
|
* set render target in the 3D API.
|
|
|
|
*/
|
|
|
|
kForceCurrentRenderTarget_FlushBit = 0x1,
|
|
|
|
/**
|
|
|
|
* A client may reach a point where it has partially rendered a frame
|
|
|
|
* through a GrContext that it knows the user will never see. This flag
|
|
|
|
* causes the flush to skip submission of deferred content to the 3D API
|
|
|
|
* during the flush.
|
|
|
|
*/
|
|
|
|
kDiscard_FlushBit = 0x2,
|
|
|
|
};
|
|
|
|
|
2011-02-22 23:59:41 +03:00
|
|
|
/**
|
|
|
|
* Call to ensure all drawing to the context has been issued to the
|
|
|
|
* underlying 3D API.
|
2011-03-10 17:13:19 +03:00
|
|
|
* @param flagsBitfield flags that control the flushing behavior. See
|
|
|
|
* FlushBits.
|
2011-02-22 23:59:41 +03:00
|
|
|
*/
|
2011-03-31 01:26:44 +04:00
|
|
|
void flush(int flagsBitfield = 0);
|
2011-11-17 00:36:03 +04:00
|
|
|
|
2011-04-05 21:08:27 +04:00
|
|
|
/**
|
|
|
|
* Reads a rectangle of pixels from a render target.
|
2011-11-17 00:36:03 +04:00
|
|
|
* @param target the render target to read from. NULL means the
|
2011-04-05 21:08:27 +04:00
|
|
|
* current render target.
|
|
|
|
* @param left left edge of the rectangle to read (inclusive)
|
|
|
|
* @param top top edge of the rectangle to read (inclusive)
|
|
|
|
* @param width width of rectangle to read in pixels.
|
|
|
|
* @param height height of rectangle to read in pixels.
|
|
|
|
* @param config the pixel config of the destination buffer
|
|
|
|
* @param buffer memory to read the rectangle into.
|
2011-11-17 00:36:03 +04:00
|
|
|
* @param rowBytes number of bytes bewtween consecutive rows. Zero
|
2011-11-02 23:57:21 +04:00
|
|
|
* means rows are tightly packed.
|
2011-04-05 21:08:27 +04:00
|
|
|
*
|
|
|
|
* @return true if the read succeeded, false if not. The read can fail
|
2011-11-17 00:36:03 +04:00
|
|
|
* because of an unsupported pixel config or because no render
|
2011-04-05 21:08:27 +04:00
|
|
|
* target is currently set.
|
|
|
|
*/
|
|
|
|
bool readRenderTargetPixels(GrRenderTarget* target,
|
|
|
|
int left, int top, int width, int height,
|
2011-11-02 23:57:21 +04:00
|
|
|
GrPixelConfig config, void* buffer,
|
2011-11-17 00:36:03 +04:00
|
|
|
size_t rowBytes) {
|
|
|
|
return this->internalReadRenderTargetPixels(target, left, top,
|
|
|
|
width, height,
|
|
|
|
config, buffer,
|
|
|
|
rowBytes, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy the src pixels [buffer, rowbytes, pixelconfig] into a render target
|
|
|
|
* at the specified rectangle.
|
|
|
|
* @param target the render target to write into. NULL means the
|
|
|
|
* current render target.
|
|
|
|
* @param left left edge of the rectangle to write (inclusive)
|
|
|
|
* @param top top edge of the rectangle to write (inclusive)
|
|
|
|
* @param width width of rectangle to write in pixels.
|
|
|
|
* @param height height of rectangle to write in pixels.
|
|
|
|
* @param config the pixel config of the source buffer
|
|
|
|
* @param buffer memory to read the rectangle from.
|
|
|
|
* @param rowBytes number of bytes bewtween consecutive rows. Zero
|
|
|
|
* means rows are tightly packed.
|
|
|
|
*/
|
|
|
|
void writeRenderTargetPixels(GrRenderTarget* target,
|
|
|
|
int left, int top, int width, int height,
|
|
|
|
GrPixelConfig config, const void* buffer,
|
|
|
|
size_t rowBytes) {
|
|
|
|
this->internalWriteRenderTargetPixels(target, left, top, width, height,
|
|
|
|
config, buffer, rowBytes, 0);
|
|
|
|
}
|
2011-04-05 21:08:27 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads a rectangle of pixels from a texture.
|
2011-11-17 00:36:03 +04:00
|
|
|
* @param texture the texture to read from.
|
2011-04-05 21:08:27 +04:00
|
|
|
* @param left left edge of the rectangle to read (inclusive)
|
|
|
|
* @param top top edge of the rectangle to read (inclusive)
|
|
|
|
* @param width width of rectangle to read in pixels.
|
|
|
|
* @param height height of rectangle to read in pixels.
|
|
|
|
* @param config the pixel config of the destination buffer
|
|
|
|
* @param buffer memory to read the rectangle into.
|
2011-11-17 00:36:03 +04:00
|
|
|
* @param rowBytes number of bytes bewtween consecutive rows. Zero
|
|
|
|
* means rows are tightly packed.
|
2011-04-05 21:08:27 +04:00
|
|
|
*
|
|
|
|
* @return true if the read succeeded, false if not. The read can fail
|
2011-11-17 00:36:03 +04:00
|
|
|
* because of an unsupported pixel config.
|
2011-02-22 23:59:41 +03:00
|
|
|
*/
|
2011-11-17 00:36:03 +04:00
|
|
|
bool readTexturePixels(GrTexture* texture,
|
2011-04-05 21:08:27 +04:00
|
|
|
int left, int top, int width, int height,
|
2011-11-17 00:36:03 +04:00
|
|
|
GrPixelConfig config, void* buffer,
|
|
|
|
size_t rowBytes) {
|
|
|
|
return this->internalReadTexturePixels(texture, left, top,
|
|
|
|
width, height,
|
|
|
|
config, buffer, rowBytes, 0);
|
|
|
|
}
|
2011-02-22 23:59:41 +03:00
|
|
|
|
|
|
|
/**
|
2011-11-17 00:36:03 +04:00
|
|
|
* Writes a rectangle of pixels to a texture.
|
|
|
|
* @param texture the render target to read from.
|
|
|
|
* @param left left edge of the rectangle to write (inclusive)
|
|
|
|
* @param top top edge of the rectangle to write (inclusive)
|
|
|
|
* @param width width of rectangle to write in pixels.
|
|
|
|
* @param height height of rectangle to write in pixels.
|
|
|
|
* @param config the pixel config of the source buffer
|
|
|
|
* @param buffer memory to read pixels from
|
|
|
|
* @param rowBytes number of bytes bewtween consecutive rows. Zero
|
|
|
|
* means rows are tightly packed.
|
2011-02-22 23:59:41 +03:00
|
|
|
*/
|
2011-11-17 00:36:03 +04:00
|
|
|
void writeTexturePixels(GrTexture* texture,
|
|
|
|
int left, int top, int width, int height,
|
|
|
|
GrPixelConfig config, const void* buffer,
|
|
|
|
size_t rowBytes) {
|
|
|
|
this->internalWriteTexturePixels(texture, left, top, width, height,
|
|
|
|
config, buffer, rowBytes, 0);
|
|
|
|
}
|
2011-12-02 23:11:17 +04:00
|
|
|
/**
|
|
|
|
* Copies all texels from one texture to another.
|
|
|
|
* @param src the texture to copy from.
|
|
|
|
* @param dst the render target to copy to.
|
|
|
|
*/
|
|
|
|
void copyTexture(GrTexture* src, GrRenderTarget* dst);
|
2012-01-31 17:35:56 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolves a render target that has MSAA. The intermediate MSAA buffer is
|
|
|
|
* downsampled to the associated GrTexture (accessible via
|
|
|
|
* GrRenderTarget::asTexture()). Any pending draws to the render target will
|
|
|
|
* be executed before the resolve.
|
|
|
|
*
|
|
|
|
* This is only necessary when a client wants to access the object directly
|
|
|
|
* using the underlying graphics API. GrContext will detect when it must
|
|
|
|
* perform a resolve to a GrTexture used as the source of a draw or before
|
|
|
|
* reading pixels back from a GrTexture or GrRenderTarget.
|
|
|
|
*/
|
|
|
|
void resolveRenderTarget(GrRenderTarget* target);
|
|
|
|
|
2011-07-08 22:03:33 +04:00
|
|
|
/**
|
2012-03-06 00:41:22 +04:00
|
|
|
* Applies a 2D Gaussian blur to a given texture.
|
|
|
|
* @param srcTexture The source texture to be blurred.
|
2012-07-18 23:52:53 +04:00
|
|
|
* @param canClobberSrc If true, srcTexture may be overwritten, and
|
|
|
|
* may be returned as the result.
|
2012-03-06 00:41:22 +04:00
|
|
|
* @param rect The destination rectangle.
|
|
|
|
* @param sigmaX The blur's standard deviation in X.
|
|
|
|
* @param sigmaY The blur's standard deviation in Y.
|
2012-07-18 23:52:53 +04:00
|
|
|
* @return the blurred texture, which may be srcTexture ref'ed, or a
|
|
|
|
* new texture. It is the caller's responsibility to unref this texture.
|
2012-03-06 00:41:22 +04:00
|
|
|
*/
|
|
|
|
GrTexture* gaussianBlur(GrTexture* srcTexture,
|
2012-07-18 23:52:53 +04:00
|
|
|
bool canClobberSrc,
|
2012-03-06 00:41:22 +04:00
|
|
|
const SkRect& rect,
|
|
|
|
float sigmaX, float sigmaY);
|
|
|
|
|
2012-05-31 22:40:36 +04:00
|
|
|
/**
|
|
|
|
* This enum is used with the function below, applyMorphology.
|
|
|
|
*/
|
|
|
|
enum MorphologyType {
|
|
|
|
kErode_MorphologyType,
|
|
|
|
kDilate_MorphologyType,
|
|
|
|
};
|
|
|
|
|
2012-03-06 00:41:22 +04:00
|
|
|
/**
|
|
|
|
* Applies a 2D morphology to a given texture.
|
|
|
|
* @param srcTexture The source texture to be blurred.
|
|
|
|
* @param rect The destination rectangle.
|
|
|
|
* @param filter The morphology filter. Must be kDilate_Filter or
|
|
|
|
* kErode_Filter.
|
|
|
|
* @param radius The morphology radius in X and Y. The filter is
|
|
|
|
* applied to a fWidth by fHeight rectangle of
|
|
|
|
* pixels.
|
2012-07-18 23:52:53 +04:00
|
|
|
* @return the morphed texture, which may be srcTexture ref'ed, or a
|
|
|
|
* new texture. It is the caller's responsibility to unref this texture.
|
2012-03-06 00:41:22 +04:00
|
|
|
*/
|
|
|
|
GrTexture* applyMorphology(GrTexture* srcTexture,
|
|
|
|
const GrRect& rect,
|
2012-05-31 22:40:36 +04:00
|
|
|
MorphologyType type,
|
2012-03-06 00:41:22 +04:00
|
|
|
SkISize radius);
|
|
|
|
|
2011-02-22 23:59:41 +03:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Helpers
|
|
|
|
|
|
|
|
class AutoRenderTarget : ::GrNoncopyable {
|
|
|
|
public:
|
|
|
|
AutoRenderTarget(GrContext* context, GrRenderTarget* target) {
|
|
|
|
fContext = NULL;
|
|
|
|
fPrevTarget = context->getRenderTarget();
|
|
|
|
if (fPrevTarget != target) {
|
|
|
|
context->setRenderTarget(target);
|
|
|
|
fContext = context;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
~AutoRenderTarget() {
|
|
|
|
if (fContext) {
|
|
|
|
fContext->setRenderTarget(fPrevTarget);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
GrContext* fContext;
|
|
|
|
GrRenderTarget* fPrevTarget;
|
|
|
|
};
|
|
|
|
|
2012-07-11 22:53:23 +04:00
|
|
|
/**
|
|
|
|
* Save/restore the view-matrix in the context.
|
|
|
|
*/
|
|
|
|
class AutoMatrix : GrNoncopyable {
|
|
|
|
public:
|
|
|
|
AutoMatrix() : fContext(NULL) {}
|
|
|
|
AutoMatrix(GrContext* ctx) : fContext(ctx) {
|
|
|
|
fMatrix = ctx->getMatrix();
|
|
|
|
}
|
|
|
|
AutoMatrix(GrContext* ctx, const GrMatrix& matrix) : fContext(ctx) {
|
|
|
|
fMatrix = ctx->getMatrix();
|
|
|
|
ctx->setMatrix(matrix);
|
|
|
|
}
|
|
|
|
void set(GrContext* ctx) {
|
|
|
|
if (NULL != fContext) {
|
|
|
|
fContext->setMatrix(fMatrix);
|
|
|
|
}
|
|
|
|
fMatrix = ctx->getMatrix();
|
|
|
|
fContext = ctx;
|
|
|
|
}
|
|
|
|
void set(GrContext* ctx, const GrMatrix& matrix) {
|
|
|
|
if (NULL != fContext) {
|
|
|
|
fContext->setMatrix(fMatrix);
|
|
|
|
}
|
|
|
|
fMatrix = ctx->getMatrix();
|
|
|
|
ctx->setMatrix(matrix);
|
|
|
|
fContext = ctx;
|
|
|
|
}
|
|
|
|
~AutoMatrix() {
|
|
|
|
if (NULL != fContext) {
|
|
|
|
fContext->setMatrix(fMatrix);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
GrContext* fContext;
|
|
|
|
GrMatrix fMatrix;
|
|
|
|
};
|
2011-02-22 23:59:41 +03:00
|
|
|
|
2012-07-12 00:57:46 +04:00
|
|
|
class AutoClip : GrNoncopyable {
|
|
|
|
public:
|
|
|
|
AutoClip(GrContext* context, const GrRect& newClipRect)
|
|
|
|
: fContext(context)
|
2012-07-26 22:52:16 +04:00
|
|
|
, fNewClipStack(newClipRect) {
|
|
|
|
fNewClipData.fClipStack = &fNewClipStack;
|
|
|
|
|
2012-07-12 00:57:46 +04:00
|
|
|
fOldClip = fContext->getClip();
|
2012-07-26 22:52:16 +04:00
|
|
|
fContext->setClip(&fNewClipData);
|
2012-07-12 00:57:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
~AutoClip() {
|
|
|
|
if (NULL != fContext) {
|
|
|
|
fContext->setClip(fOldClip);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private:
|
2012-07-26 22:52:16 +04:00
|
|
|
GrContext* fContext;
|
|
|
|
const GrClipData* fOldClip;
|
|
|
|
|
|
|
|
GrClip fNewClipStack;
|
|
|
|
GrClipData fNewClipData;
|
2012-07-12 00:57:46 +04:00
|
|
|
};
|
|
|
|
|
2011-02-22 23:59:41 +03:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Functions intended for internal use only.
|
|
|
|
GrGpu* getGpu() { return fGpu; }
|
2011-08-24 00:54:07 +04:00
|
|
|
const GrGpu* getGpu() const { return fGpu; }
|
2011-02-22 23:59:41 +03:00
|
|
|
GrFontCache* getFontCache() { return fFontCache; }
|
|
|
|
GrDrawTarget* getTextTarget(const GrPaint& paint);
|
|
|
|
const GrIndexBuffer* getQuadIndexBuffer() const;
|
2012-06-06 22:21:18 +04:00
|
|
|
|
2011-08-08 21:01:14 +04:00
|
|
|
/**
|
|
|
|
* Stencil buffers add themselves to the cache using
|
|
|
|
* addAndLockStencilBuffer. When a SB's RT-attachment count
|
|
|
|
* reaches zero the SB unlocks itself using unlockStencilBuffer and is
|
|
|
|
* eligible for purging. findStencilBuffer is called to check the cache for
|
|
|
|
* a SB that matching an RT's criteria. If a match is found that has been
|
|
|
|
* unlocked (its attachment count has reached 0) then it will be relocked.
|
|
|
|
*/
|
|
|
|
GrResourceEntry* addAndLockStencilBuffer(GrStencilBuffer* sb);
|
|
|
|
void unlockStencilBuffer(GrResourceEntry* sbEntry);
|
|
|
|
GrStencilBuffer* findStencilBuffer(int width, int height, int sampleCnt);
|
2011-02-22 23:59:41 +03:00
|
|
|
|
2012-05-23 00:28:23 +04:00
|
|
|
GrPathRenderer* getPathRenderer(const SkPath& path,
|
|
|
|
GrPathFill fill,
|
|
|
|
const GrDrawTarget* target,
|
2012-05-23 17:19:12 +04:00
|
|
|
bool antiAlias,
|
|
|
|
bool allowSW);
|
2012-05-23 00:28:23 +04:00
|
|
|
|
2011-02-22 23:59:41 +03:00
|
|
|
private:
|
|
|
|
// used to keep track of when we need to flush the draw buffer
|
|
|
|
enum DrawCategory {
|
|
|
|
kBuffered_DrawCategory, // last draw was inserted in draw buffer
|
|
|
|
kUnbuffered_DrawCategory, // last draw was not inserted in the draw buffer
|
|
|
|
};
|
|
|
|
DrawCategory fLastDrawCategory;
|
|
|
|
|
2011-07-27 00:45:30 +04:00
|
|
|
GrGpu* fGpu;
|
2012-03-30 18:35:04 +04:00
|
|
|
GrDrawState* fDrawState;
|
|
|
|
|
2011-07-27 00:45:30 +04:00
|
|
|
GrResourceCache* fTextureCache;
|
|
|
|
GrFontCache* fFontCache;
|
2011-03-25 15:31:16 +03:00
|
|
|
|
2011-08-19 19:42:31 +04:00
|
|
|
GrPathRendererChain* fPathRendererChain;
|
2012-05-23 17:19:12 +04:00
|
|
|
GrSoftwarePathRenderer* fSoftwarePathRenderer;
|
2011-02-22 23:59:41 +03:00
|
|
|
|
|
|
|
GrVertexBufferAllocPool* fDrawBufferVBAllocPool;
|
|
|
|
GrIndexBufferAllocPool* fDrawBufferIBAllocPool;
|
|
|
|
GrInOrderDrawBuffer* fDrawBuffer;
|
|
|
|
|
2012-06-15 17:58:07 +04:00
|
|
|
GrAARectRenderer* fAARectRenderer;
|
2011-03-25 15:31:16 +03:00
|
|
|
|
2012-06-15 17:58:07 +04:00
|
|
|
GrContext(GrGpu* gpu);
|
2011-04-25 16:43:45 +04:00
|
|
|
|
2011-03-31 01:26:44 +04:00
|
|
|
void setupDrawBuffer();
|
|
|
|
|
2011-02-22 23:59:41 +03:00
|
|
|
void flushDrawBuffer();
|
|
|
|
|
2012-03-30 18:35:04 +04:00
|
|
|
void setPaint(const GrPaint& paint);
|
2011-02-22 23:59:41 +03:00
|
|
|
|
|
|
|
GrDrawTarget* prepareToDraw(const GrPaint& paint, DrawCategory drawType);
|
|
|
|
|
2012-04-27 19:52:53 +04:00
|
|
|
void internalDrawPath(const GrPaint& paint, const SkPath& path,
|
2012-04-27 17:05:21 +04:00
|
|
|
GrPathFill fill, const GrPoint* translate);
|
|
|
|
|
2011-11-17 00:36:03 +04:00
|
|
|
/**
|
|
|
|
* Flags to the internal read/write pixels funcs
|
|
|
|
*/
|
|
|
|
enum PixelOpsFlags {
|
|
|
|
kDontFlush_PixelOpsFlag = 0x1,
|
|
|
|
};
|
|
|
|
|
|
|
|
bool internalReadRenderTargetPixels(GrRenderTarget* target,
|
|
|
|
int left, int top,
|
|
|
|
int width, int height,
|
|
|
|
GrPixelConfig config, void* buffer,
|
|
|
|
size_t rowBytes, uint32_t flags);
|
|
|
|
|
|
|
|
void internalWriteRenderTargetPixels(GrRenderTarget* target,
|
|
|
|
int left, int top,
|
|
|
|
int width, int height,
|
|
|
|
GrPixelConfig, const void* buffer,
|
|
|
|
size_t rowBytes, uint32_t flags);
|
|
|
|
|
|
|
|
bool internalReadTexturePixels(GrTexture* texture,
|
|
|
|
int left, int top,
|
|
|
|
int width, int height,
|
|
|
|
GrPixelConfig config, void* buffer,
|
|
|
|
size_t rowBytes, uint32_t flags);
|
|
|
|
|
|
|
|
void internalWriteTexturePixels(GrTexture* texture,
|
|
|
|
int left, int top,
|
|
|
|
int width, int height,
|
|
|
|
GrPixelConfig config, const void* buffer,
|
|
|
|
size_t rowBytes, uint32_t flags);
|
|
|
|
// needed for access to internalWriteTexturePixels. TODO: make GrContext
|
|
|
|
// be a facade for an internal class. Then functions that are public on the
|
|
|
|
// internal class would have only be callable in src/gpu. The facade would
|
|
|
|
// only have to functions necessary for clients.
|
|
|
|
friend class GrAtlas;
|
|
|
|
|
2012-06-22 16:41:43 +04:00
|
|
|
// Needed so GrTexture's returnToCache helper function can call
|
|
|
|
// addExistingTextureToCache
|
|
|
|
friend class GrTexture;
|
|
|
|
|
|
|
|
// Add an existing texture to the texture cache. This is intended solely
|
|
|
|
// for use with textures released from an GrAutoScratchTexture.
|
|
|
|
void addExistingTextureToCache(GrTexture* texture);
|
2012-06-27 00:16:17 +04:00
|
|
|
|
|
|
|
typedef GrRefCnt INHERITED;
|
2011-02-22 23:59:41 +03:00
|
|
|
};
|
|
|
|
|
2011-07-12 23:44:51 +04:00
|
|
|
/**
|
2011-07-27 00:45:30 +04:00
|
|
|
* Gets and locks a scratch texture from a descriptor using
|
|
|
|
* either exact or approximate criteria. Unlocks texture in
|
|
|
|
* the destructor.
|
2011-07-12 23:44:51 +04:00
|
|
|
*/
|
2011-07-27 00:45:30 +04:00
|
|
|
class GrAutoScratchTexture : ::GrNoncopyable {
|
2011-07-12 23:44:51 +04:00
|
|
|
public:
|
2011-07-27 00:45:30 +04:00
|
|
|
GrAutoScratchTexture()
|
|
|
|
: fContext(NULL) {
|
2011-07-12 23:44:51 +04:00
|
|
|
}
|
2011-07-27 00:45:30 +04:00
|
|
|
|
|
|
|
GrAutoScratchTexture(GrContext* context,
|
|
|
|
const GrTextureDesc& desc,
|
|
|
|
GrContext::ScratchTexMatch match =
|
|
|
|
GrContext::kApprox_ScratchTexMatch)
|
|
|
|
: fContext(NULL) {
|
|
|
|
this->set(context, desc, match);
|
|
|
|
}
|
|
|
|
|
|
|
|
~GrAutoScratchTexture() {
|
2012-06-22 16:01:30 +04:00
|
|
|
this->reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset() {
|
2012-06-22 16:41:43 +04:00
|
|
|
if (NULL != fContext && NULL != fEntry.cacheEntry()) {
|
2011-07-12 23:44:51 +04:00
|
|
|
fContext->unlockTexture(fEntry);
|
2012-06-22 16:01:30 +04:00
|
|
|
fEntry.reset();
|
2011-07-12 23:44:51 +04:00
|
|
|
}
|
|
|
|
}
|
2011-07-14 18:45:44 +04:00
|
|
|
|
2012-06-22 16:41:43 +04:00
|
|
|
/*
|
|
|
|
* When detaching a texture we do not unlock it in the texture cache but
|
|
|
|
* we do set the returnToCache flag. In this way the texture remains
|
|
|
|
* "locked" in the texture cache until it is freed and recycled in
|
|
|
|
* GrTexture::internal_dispose. In reality, the texture has been removed
|
|
|
|
* from the cache (because this is in AutoScratchTexture) and by not
|
|
|
|
* calling unlockTexture we simply don't re-add it. It will be reattached
|
|
|
|
* in GrTexture::internal_dispose.
|
|
|
|
*
|
|
|
|
* Note that the caller is assumed to accept and manage the ref to the
|
|
|
|
* returned texture.
|
|
|
|
*/
|
|
|
|
GrTexture* detach() {
|
|
|
|
GrTexture* temp = this->texture();
|
|
|
|
|
|
|
|
GrAssert(1 == temp->getRefCnt());
|
|
|
|
|
|
|
|
// freeEntry will remove the texture cache's ref
|
|
|
|
temp->ref();
|
|
|
|
fContext->freeEntry(fEntry);
|
|
|
|
fEntry.reset();
|
|
|
|
|
|
|
|
temp->setFlag((GrTextureFlags) GrTexture::kReturnToCache_FlagBit);
|
|
|
|
GrAssert(1 == temp->getRefCnt());
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
2011-07-27 00:45:30 +04:00
|
|
|
GrTexture* set(GrContext* context,
|
|
|
|
const GrTextureDesc& desc,
|
|
|
|
GrContext::ScratchTexMatch match =
|
|
|
|
GrContext::kApprox_ScratchTexMatch) {
|
2012-06-22 16:01:30 +04:00
|
|
|
this->reset();
|
|
|
|
|
2011-07-27 00:45:30 +04:00
|
|
|
fContext = context;
|
|
|
|
if (NULL != fContext) {
|
|
|
|
fEntry = fContext->lockScratchTexture(desc, match);
|
|
|
|
GrTexture* ret = fEntry.texture();
|
|
|
|
if (NULL == ret) {
|
|
|
|
fContext = NULL;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GrTexture* texture() { return fEntry.texture(); }
|
2011-07-12 23:44:51 +04:00
|
|
|
private:
|
2011-07-27 00:45:30 +04:00
|
|
|
GrContext* fContext;
|
|
|
|
GrContext::TextureCacheEntry fEntry;
|
2011-07-12 23:44:51 +04:00
|
|
|
};
|
|
|
|
|
2011-02-22 23:59:41 +03:00
|
|
|
#endif
|