michael@0: /* michael@0: * Copyright 2010 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #ifndef GrContext_DEFINED michael@0: #define GrContext_DEFINED michael@0: michael@0: #include "GrClipData.h" michael@0: #include "GrColor.h" michael@0: #include "GrPaint.h" michael@0: #include "GrPathRendererChain.h" michael@0: #include "GrPoint.h" michael@0: #include "GrRenderTarget.h" michael@0: #include "GrTexture.h" michael@0: #include "SkMatrix.h" michael@0: #include "SkTypes.h" michael@0: michael@0: class GrAARectRenderer; michael@0: class GrAutoScratchTexture; michael@0: class GrDrawState; michael@0: class GrDrawTarget; michael@0: class GrEffect; michael@0: class GrFontCache; michael@0: class GrGpu; michael@0: class GrIndexBuffer; michael@0: class GrIndexBufferAllocPool; michael@0: class GrInOrderDrawBuffer; michael@0: class GrOvalRenderer; michael@0: class GrPath; michael@0: class GrPathRenderer; michael@0: class GrResourceEntry; michael@0: class GrResourceCache; michael@0: class GrStencilBuffer; michael@0: class GrTestTarget; michael@0: class GrTextureParams; michael@0: class GrVertexBuffer; michael@0: class GrVertexBufferAllocPool; michael@0: class GrSoftwarePathRenderer; michael@0: class SkStrokeRec; michael@0: michael@0: class SK_API GrContext : public SkRefCnt { michael@0: public: michael@0: SK_DECLARE_INST_COUNT(GrContext) michael@0: michael@0: /** michael@0: * Creates a GrContext for a backend context. michael@0: */ michael@0: static GrContext* Create(GrBackend, GrBackendContext); michael@0: michael@0: virtual ~GrContext(); michael@0: michael@0: /** michael@0: * The GrContext normally assumes that no outsider is setting state michael@0: * within the underlying 3D API's context/device/whatever. This call informs michael@0: * the context that the state was modified and it should resend. Shouldn't michael@0: * be called frequently for good performance. michael@0: * The flag bits, state, is dpendent on which backend is used by the michael@0: * context, either GL or D3D (possible in future). michael@0: */ michael@0: void resetContext(uint32_t state = kAll_GrBackendState); michael@0: michael@0: /** michael@0: * Callback function to allow classes to cleanup on GrContext destruction. michael@0: * The 'info' field is filled in with the 'info' passed to addCleanUp. michael@0: */ michael@0: typedef void (*PFCleanUpFunc)(const GrContext* context, void* info); michael@0: michael@0: /** michael@0: * Add a function to be called from within GrContext's destructor. michael@0: * This gives classes a chance to free resources held on a per context basis. michael@0: * The 'info' parameter will be stored and passed to the callback function. michael@0: */ michael@0: void addCleanUp(PFCleanUpFunc cleanUp, void* info) { michael@0: CleanUpData* entry = fCleanUpData.push(); michael@0: michael@0: entry->fFunc = cleanUp; michael@0: entry->fInfo = info; michael@0: } michael@0: michael@0: /** michael@0: * Abandons all GPU resources, assumes 3D API state is unknown. Call this michael@0: * if you have lost the associated GPU context, and thus internal texture, michael@0: * buffer, etc. references/IDs are now invalid. Should be called even when michael@0: * GrContext is no longer going to be used for two reasons: michael@0: * 1) ~GrContext will not try to free the objects in the 3D API. michael@0: * 2) If you've created GrResources that outlive the GrContext they will michael@0: * be marked as invalid (GrResource::isValid()) and won't attempt to michael@0: * free their underlying resource in the 3D API. michael@0: * Content drawn since the last GrContext::flush() may be lost. michael@0: */ michael@0: void contextLost(); michael@0: michael@0: /** michael@0: * Similar to contextLost, but makes no attempt to reset state. michael@0: * Use this method when GrContext destruction is pending, but michael@0: * the graphics context is destroyed first. michael@0: */ michael@0: void contextDestroyed(); michael@0: michael@0: /** michael@0: * Frees GPU created by the context. Can be called to reduce GPU memory michael@0: * pressure. michael@0: */ michael@0: void freeGpuResources(); michael@0: michael@0: /** michael@0: * Returns the number of bytes of GPU memory hosted by the texture cache. michael@0: */ michael@0: size_t getGpuTextureCacheBytes() const; michael@0: michael@0: /////////////////////////////////////////////////////////////////////////// michael@0: // Textures michael@0: michael@0: /** michael@0: * Creates a new entry, based on the specified key and texture and returns it. The caller owns a michael@0: * ref on the returned texture which must be balanced by a call to unref. michael@0: * michael@0: * @param params The texture params used to draw a texture may help determine michael@0: * the cache entry used. (e.g. different versions may exist michael@0: * for different wrap modes on GPUs with limited NPOT michael@0: * texture support). NULL implies clamp wrap modes. michael@0: * @param desc Description of the texture properties. michael@0: * @param cacheID Cache-specific properties (e.g., texture gen ID) michael@0: * @param srcData Pointer to the pixel values. michael@0: * @param rowBytes The number of bytes between rows of the texture. Zero michael@0: * implies tightly packed rows. michael@0: * @param cacheKey (optional) If non-NULL, we'll write the cache key we used to cacheKey. michael@0: */ michael@0: GrTexture* createTexture(const GrTextureParams* params, michael@0: const GrTextureDesc& desc, michael@0: const GrCacheID& cacheID, michael@0: void* srcData, michael@0: size_t rowBytes, michael@0: GrResourceKey* cacheKey = NULL); michael@0: michael@0: /** michael@0: * Search for an entry based on key and dimensions. If found, ref it and return it. The return michael@0: * value will be NULL if not found. The caller must balance with a call to unref. michael@0: * michael@0: * @param desc Description of the texture properties. michael@0: * @param cacheID Cache-specific properties (e.g., texture gen ID) michael@0: * @param params The texture params used to draw a texture may help determine michael@0: * the cache entry used. (e.g. different versions may exist michael@0: * for different wrap modes on GPUs with limited NPOT michael@0: * texture support). NULL implies clamp wrap modes. michael@0: */ michael@0: GrTexture* findAndRefTexture(const GrTextureDesc& desc, michael@0: const GrCacheID& cacheID, michael@0: const GrTextureParams* params); michael@0: /** michael@0: * Determines whether a texture is in the cache. If the texture is found it michael@0: * will not be locked or returned. This call does not affect the priority of michael@0: * the texture for deletion. michael@0: */ michael@0: bool isTextureInCache(const GrTextureDesc& desc, michael@0: const GrCacheID& cacheID, michael@0: const GrTextureParams* params) const; michael@0: michael@0: /** michael@0: * Enum that determines how closely a returned scratch texture must match michael@0: * a provided GrTextureDesc. michael@0: */ michael@0: enum ScratchTexMatch { michael@0: /** michael@0: * Finds a texture that exactly matches the descriptor. michael@0: */ michael@0: kExact_ScratchTexMatch, michael@0: /** michael@0: * Finds a texture that approximately matches the descriptor. Will be michael@0: * at least as large in width and height as desc specifies. If desc michael@0: * specifies that texture is a render target then result will be a michael@0: * render target. If desc specifies a render target and doesn't set the michael@0: * no stencil flag then result will have a stencil. Format and aa level michael@0: * will always match. michael@0: */ michael@0: kApprox_ScratchTexMatch michael@0: }; michael@0: michael@0: /** michael@0: * Returns a texture matching the desc. It's contents are unknown. Subsequent michael@0: * requests with the same descriptor are not guaranteed to return the same michael@0: * texture. The same texture is guaranteed not be returned again until it is michael@0: * unlocked. Call must be balanced with an unlockTexture() call. The caller michael@0: * owns a ref on the returned texture and must balance with a call to unref. michael@0: * michael@0: * Textures created by createAndLockTexture() hide the complications of michael@0: * tiling non-power-of-two textures on APIs that don't support this (e.g. michael@0: * unextended GLES2). Tiling a NPOT texture created by lockScratchTexture on michael@0: * such an API will create gaps in the tiling pattern. This includes clamp michael@0: * mode. (This may be addressed in a future update.) michael@0: */ michael@0: GrTexture* lockAndRefScratchTexture(const GrTextureDesc&, ScratchTexMatch match); michael@0: michael@0: /** michael@0: * When done with an entry, call unlockScratchTexture(entry) on it, which returns michael@0: * it to the cache, where it may be purged. This does not unref the texture. michael@0: */ michael@0: void unlockScratchTexture(GrTexture* texture); michael@0: michael@0: /** michael@0: * This method should be called whenever a GrTexture is unreffed or michael@0: * switched from exclusive to non-exclusive. This michael@0: * gives the resource cache a chance to discard unneeded textures. michael@0: * Note: this entry point will be removed once totally ref-driven michael@0: * cache maintenance is implemented michael@0: */ michael@0: void purgeCache(); michael@0: michael@0: /** michael@0: * Purge all the unlocked resources from the cache. michael@0: * This entry point is mainly meant for timing texture uploads michael@0: * and is not defined in normal builds of Skia. michael@0: */ michael@0: void purgeAllUnlockedResources(); michael@0: michael@0: /** michael@0: * Creates a texture that is outside the cache. Does not count against michael@0: * cache's budget. michael@0: */ michael@0: GrTexture* createUncachedTexture(const GrTextureDesc& desc, michael@0: void* srcData, michael@0: size_t rowBytes); michael@0: michael@0: /** michael@0: * Returns true if the specified use of an indexed texture is supported. michael@0: * Support may depend upon whether the texture params indicate that the michael@0: * texture will be tiled. Passing NULL for the texture params indicates michael@0: * clamp mode. michael@0: */ michael@0: bool supportsIndex8PixelConfig(const GrTextureParams*, michael@0: int width, michael@0: int height) const; michael@0: michael@0: /** michael@0: * Return the current texture cache limits. michael@0: * michael@0: * @param maxTextures If non-null, returns maximum number of textures that michael@0: * can be held in the cache. michael@0: * @param maxTextureBytes If non-null, returns maximum number of bytes of michael@0: * texture memory that can be held in the cache. michael@0: */ michael@0: void getTextureCacheLimits(int* maxTextures, size_t* maxTextureBytes) const; michael@0: michael@0: /** michael@0: * Specify the texture cache limits. If the current cache exceeds either michael@0: * of these, it will be purged (LRU) to keep the cache within these limits. michael@0: * michael@0: * @param maxTextures The maximum number of textures that can be held in michael@0: * the cache. michael@0: * @param maxTextureBytes The maximum number of bytes of texture memory michael@0: * that can be held in the cache. michael@0: */ michael@0: void setTextureCacheLimits(int maxTextures, size_t maxTextureBytes); michael@0: michael@0: /** michael@0: * Return the max width or height of a texture supported by the current GPU. michael@0: */ michael@0: int getMaxTextureSize() const; michael@0: michael@0: /** michael@0: * Temporarily override the true max texture size. Note: an override michael@0: * larger then the true max texture size will have no effect. michael@0: * This entry point is mainly meant for testing texture size dependent michael@0: * features and is only available if defined outside of Skia (see michael@0: * bleed GM. michael@0: */ michael@0: void setMaxTextureSizeOverride(int maxTextureSizeOverride); michael@0: michael@0: /////////////////////////////////////////////////////////////////////////// michael@0: // Render targets michael@0: michael@0: /** michael@0: * Sets the render target. michael@0: * @param target the render target to set. michael@0: */ michael@0: void setRenderTarget(GrRenderTarget* target) { michael@0: fRenderTarget.reset(SkSafeRef(target)); michael@0: } michael@0: michael@0: /** michael@0: * Gets the current render target. michael@0: * @return the currently bound render target. michael@0: */ michael@0: const GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); } michael@0: GrRenderTarget* getRenderTarget() { return fRenderTarget.get(); } michael@0: michael@0: GrAARectRenderer* getAARectRenderer() { return fAARectRenderer; } michael@0: michael@0: /** michael@0: * Can the provided configuration act as a color render target? michael@0: */ michael@0: bool isConfigRenderable(GrPixelConfig config, bool withMSAA) const; michael@0: michael@0: /** michael@0: * Return the max width or height of a render target supported by the michael@0: * current GPU. michael@0: */ michael@0: int getMaxRenderTargetSize() const; michael@0: michael@0: /** michael@0: * Returns the max sample count for a render target. It will be 0 if MSAA michael@0: * is not supported. michael@0: */ michael@0: int getMaxSampleCount() const; michael@0: michael@0: /** michael@0: * Returns the recommended sample count for a render target when using this michael@0: * context. michael@0: * michael@0: * @param config the configuration of the render target. michael@0: * @param dpi the display density in dots per inch. michael@0: * michael@0: * @return sample count that should be perform well and have good enough michael@0: * rendering quality for the display. Alternatively returns 0 if michael@0: * MSAA is not supported or recommended to be used by default. michael@0: */ michael@0: int getRecommendedSampleCount(GrPixelConfig config, SkScalar dpi) const; michael@0: michael@0: /////////////////////////////////////////////////////////////////////////// michael@0: // Backend Surfaces michael@0: michael@0: /** michael@0: * Wraps an existing texture with a GrTexture object. michael@0: * michael@0: * OpenGL: if the object is a texture Gr may change its GL texture params michael@0: * when it is drawn. michael@0: * michael@0: * @param desc description of the object to create. michael@0: * michael@0: * @return GrTexture object or NULL on failure. michael@0: */ michael@0: GrTexture* wrapBackendTexture(const GrBackendTextureDesc& desc); michael@0: michael@0: /** michael@0: * Wraps an existing render target with a GrRenderTarget object. It is michael@0: * similar to wrapBackendTexture but can be used to draw into surfaces michael@0: * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that michael@0: * the client will resolve to a texture). michael@0: * michael@0: * @param desc description of the object to create. michael@0: * michael@0: * @return GrTexture object or NULL on failure. michael@0: */ michael@0: GrRenderTarget* wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc); michael@0: michael@0: /////////////////////////////////////////////////////////////////////////// michael@0: // Matrix state michael@0: michael@0: /** michael@0: * Gets the current transformation matrix. michael@0: * @return the current matrix. michael@0: */ michael@0: const SkMatrix& getMatrix() const { return fViewMatrix; } michael@0: michael@0: /** michael@0: * Sets the transformation matrix. michael@0: * @param m the matrix to set. michael@0: */ michael@0: void setMatrix(const SkMatrix& m) { fViewMatrix = m; } michael@0: michael@0: /** michael@0: * Sets the current transformation matrix to identity. michael@0: */ michael@0: void setIdentityMatrix() { fViewMatrix.reset(); } michael@0: michael@0: /** michael@0: * Concats the current matrix. The passed matrix is applied before the michael@0: * current matrix. michael@0: * @param m the matrix to concat. michael@0: */ michael@0: void concatMatrix(const SkMatrix& m) { fViewMatrix.preConcat(m); } michael@0: michael@0: michael@0: /////////////////////////////////////////////////////////////////////////// michael@0: // Clip state michael@0: /** michael@0: * Gets the current clip. michael@0: * @return the current clip. michael@0: */ michael@0: const GrClipData* getClip() const { return fClip; } michael@0: michael@0: /** michael@0: * Sets the clip. michael@0: * @param clipData the clip to set. michael@0: */ michael@0: void setClip(const GrClipData* clipData) { fClip = clipData; } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////// michael@0: // Draws michael@0: michael@0: /** michael@0: * Clear the entire or rect of the render target, ignoring any clips. michael@0: * @param rect the rect to clear or the whole thing if rect is NULL. michael@0: * @param color the color to clear to. michael@0: * @param canIgnoreRect allows partial clears to be converted to whole michael@0: * clears on platforms for which that is cheap michael@0: * @param target if non-NULL, the render target to clear otherwise clear michael@0: * the current render target michael@0: */ michael@0: void clear(const SkIRect* rect, GrColor color, bool canIgnoreRect, michael@0: GrRenderTarget* target = NULL); michael@0: michael@0: /** michael@0: * Draw everywhere (respecting the clip) with the paint. michael@0: */ michael@0: void drawPaint(const GrPaint& paint); michael@0: michael@0: /** michael@0: * Draw the rect using a paint. michael@0: * @param paint describes how to color pixels. michael@0: * @param stroke the stroke information (width, join, cap). michael@0: * If stroke == NULL, then the rect is filled. michael@0: * Otherwise, if stroke width == 0, then the stroke michael@0: * is always a single pixel thick, else the rect is michael@0: * mitered/beveled stroked based on stroke width. michael@0: * @param matrix Optional matrix applied to the rect. Applied before michael@0: * context's matrix or the paint's matrix. michael@0: * The rects coords are used to access the paint (through texture matrix) michael@0: */ michael@0: void drawRect(const GrPaint& paint, michael@0: const SkRect&, michael@0: const SkStrokeRec* stroke = NULL, michael@0: const SkMatrix* matrix = NULL); michael@0: michael@0: /** michael@0: * Maps a rect of local coordinates onto the a rect of destination michael@0: * coordinates. Each rect can optionally be transformed. The localRect michael@0: * is stretched over the dstRect. The dstRect is transformed by the michael@0: * context's matrix. Additional optional matrices for both rects can be michael@0: * provided by parameters. michael@0: * michael@0: * @param paint describes how to color pixels. michael@0: * @param dstRect the destination rect to draw. michael@0: * @param localRect rect of local coordinates to be mapped onto dstRect michael@0: * @param dstMatrix Optional matrix to transform dstRect. Applied before context's matrix. michael@0: * @param localMatrix Optional matrix to transform localRect. michael@0: */ michael@0: void drawRectToRect(const GrPaint& paint, michael@0: const SkRect& dstRect, michael@0: const SkRect& localRect, michael@0: const SkMatrix* dstMatrix = NULL, michael@0: const SkMatrix* localMatrix = NULL); michael@0: michael@0: /** michael@0: * Draw a roundrect using a paint. michael@0: * michael@0: * @param paint describes how to color pixels. michael@0: * @param rrect the roundrect to draw michael@0: * @param stroke the stroke information (width, join, cap) michael@0: */ michael@0: void drawRRect(const GrPaint& paint, michael@0: const SkRRect& rrect, michael@0: const SkStrokeRec& stroke); michael@0: michael@0: /** michael@0: * Draws a path. michael@0: * michael@0: * @param paint describes how to color pixels. michael@0: * @param path the path to draw michael@0: * @param stroke the stroke information (width, join, cap) michael@0: */ michael@0: void drawPath(const GrPaint& paint, const SkPath& path, const SkStrokeRec& stroke); michael@0: michael@0: /** michael@0: * Draws vertices with a paint. michael@0: * michael@0: * @param paint describes how to color pixels. michael@0: * @param primitiveType primitives type to draw. michael@0: * @param vertexCount number of vertices. michael@0: * @param positions array of vertex positions, required. michael@0: * @param texCoords optional array of texture coordinates used michael@0: * to access the paint. michael@0: * @param colors optional array of per-vertex colors, supercedes michael@0: * the paint's color field. michael@0: * @param indices optional array of indices. If NULL vertices michael@0: * are drawn non-indexed. michael@0: * @param indexCount if indices is non-null then this is the michael@0: * number of indices. michael@0: */ michael@0: void drawVertices(const GrPaint& paint, michael@0: GrPrimitiveType primitiveType, michael@0: int vertexCount, michael@0: const GrPoint positions[], michael@0: const GrPoint texs[], michael@0: const GrColor colors[], michael@0: const uint16_t indices[], michael@0: int indexCount); michael@0: michael@0: /** michael@0: * Draws an oval. michael@0: * michael@0: * @param paint describes how to color pixels. michael@0: * @param oval the bounding rect of the oval. michael@0: * @param stroke the stroke information (width, style) michael@0: */ michael@0: void drawOval(const GrPaint& paint, michael@0: const SkRect& oval, michael@0: const SkStrokeRec& stroke); michael@0: michael@0: /////////////////////////////////////////////////////////////////////////// michael@0: // Misc. michael@0: michael@0: /** michael@0: * Flags that affect flush() behavior. michael@0: */ michael@0: enum FlushBits { michael@0: /** michael@0: * A client may reach a point where it has partially rendered a frame michael@0: * through a GrContext that it knows the user will never see. This flag michael@0: * causes the flush to skip submission of deferred content to the 3D API michael@0: * during the flush. michael@0: */ michael@0: kDiscard_FlushBit = 0x2, michael@0: }; michael@0: michael@0: /** michael@0: * Call to ensure all drawing to the context has been issued to the michael@0: * underlying 3D API. michael@0: * @param flagsBitfield flags that control the flushing behavior. See michael@0: * FlushBits. michael@0: */ michael@0: void flush(int flagsBitfield = 0); michael@0: michael@0: /** michael@0: * These flags can be used with the read/write pixels functions below. michael@0: */ michael@0: enum PixelOpsFlags { michael@0: /** The GrContext will not be flushed. This means that the read or write may occur before michael@0: previous draws have executed. */ michael@0: kDontFlush_PixelOpsFlag = 0x1, michael@0: /** The src for write or dst read is unpremultiplied. This is only respected if both the michael@0: config src and dst configs are an RGBA/BGRA 8888 format. */ michael@0: kUnpremul_PixelOpsFlag = 0x2, michael@0: }; michael@0: michael@0: /** michael@0: * Reads a rectangle of pixels from a render target. michael@0: * @param target the render target to read from. NULL means the current render target. michael@0: * @param left left edge of the rectangle to read (inclusive) michael@0: * @param top top edge of the rectangle to read (inclusive) michael@0: * @param width width of rectangle to read in pixels. michael@0: * @param height height of rectangle to read in pixels. michael@0: * @param config the pixel config of the destination buffer michael@0: * @param buffer memory to read the rectangle into. michael@0: * @param rowBytes number of bytes bewtween consecutive rows. Zero means rows are tightly michael@0: * packed. michael@0: * @param pixelOpsFlags see PixelOpsFlags enum above. michael@0: * michael@0: * @return true if the read succeeded, false if not. The read can fail because of an unsupported michael@0: * pixel config or because no render target is currently set and NULL was passed for michael@0: * target. michael@0: */ michael@0: bool readRenderTargetPixels(GrRenderTarget* target, michael@0: int left, int top, int width, int height, michael@0: GrPixelConfig config, void* buffer, michael@0: size_t rowBytes = 0, michael@0: uint32_t pixelOpsFlags = 0); michael@0: michael@0: /** michael@0: * Copy the src pixels [buffer, row bytes, pixel config] into a render target at the specified michael@0: * rectangle. michael@0: * @param target the render target to write into. NULL means the current render target. michael@0: * @param left left edge of the rectangle to write (inclusive) michael@0: * @param top top edge of the rectangle to write (inclusive) michael@0: * @param width width of rectangle to write in pixels. michael@0: * @param height height of rectangle to write in pixels. michael@0: * @param config the pixel config of the source buffer michael@0: * @param buffer memory to read the rectangle from. michael@0: * @param rowBytes number of bytes between consecutive rows. Zero means rows are tightly michael@0: * packed. michael@0: * @param pixelOpsFlags see PixelOpsFlags enum above. michael@0: * michael@0: * @return true if the write succeeded, false if not. The write can fail because of an michael@0: * unsupported combination of target and pixel configs. michael@0: */ michael@0: bool writeRenderTargetPixels(GrRenderTarget* target, michael@0: int left, int top, int width, int height, michael@0: GrPixelConfig config, const void* buffer, michael@0: size_t rowBytes = 0, michael@0: uint32_t pixelOpsFlags = 0); michael@0: michael@0: /** michael@0: * Reads a rectangle of pixels from a texture. michael@0: * @param texture the texture to read from. michael@0: * @param left left edge of the rectangle to read (inclusive) michael@0: * @param top top edge of the rectangle to read (inclusive) michael@0: * @param width width of rectangle to read in pixels. michael@0: * @param height height of rectangle to read in pixels. michael@0: * @param config the pixel config of the destination buffer michael@0: * @param buffer memory to read the rectangle into. michael@0: * @param rowBytes number of bytes between consecutive rows. Zero means rows are tightly michael@0: * packed. michael@0: * @param pixelOpsFlags see PixelOpsFlags enum above. michael@0: * michael@0: * @return true if the read succeeded, false if not. The read can fail because of an unsupported michael@0: * pixel config. michael@0: */ michael@0: bool readTexturePixels(GrTexture* texture, michael@0: int left, int top, int width, int height, michael@0: GrPixelConfig config, void* buffer, michael@0: size_t rowBytes = 0, michael@0: uint32_t pixelOpsFlags = 0); michael@0: michael@0: /** michael@0: * Writes a rectangle of pixels to a texture. michael@0: * @param texture the render target to read from. michael@0: * @param left left edge of the rectangle to write (inclusive) michael@0: * @param top top edge of the rectangle to write (inclusive) michael@0: * @param width width of rectangle to write in pixels. michael@0: * @param height height of rectangle to write in pixels. michael@0: * @param config the pixel config of the source buffer michael@0: * @param buffer memory to read pixels from michael@0: * @param rowBytes number of bytes between consecutive rows. Zero michael@0: * means rows are tightly packed. michael@0: * @param pixelOpsFlags see PixelOpsFlags enum above. michael@0: * @return true if the write succeeded, false if not. The write can fail because of an michael@0: * unsupported combination of texture and pixel configs. michael@0: */ michael@0: bool writeTexturePixels(GrTexture* texture, michael@0: int left, int top, int width, int height, michael@0: GrPixelConfig config, const void* buffer, michael@0: size_t rowBytes, michael@0: uint32_t pixelOpsFlags = 0); michael@0: michael@0: michael@0: /** michael@0: * Copies a rectangle of texels from src to dst. The size of dst is the size of the rectangle michael@0: * copied and topLeft is the position of the rect in src. The rectangle is clipped to src's michael@0: * bounds. michael@0: * @param src the texture to copy from. michael@0: * @param dst the render target to copy to. michael@0: * @param topLeft the point in src that will be copied to the top-left of dst. If NULL, michael@0: * (0, 0) will be used. michael@0: */ michael@0: void copyTexture(GrTexture* src, GrRenderTarget* dst, const SkIPoint* topLeft = NULL); michael@0: michael@0: /** michael@0: * Resolves a render target that has MSAA. The intermediate MSAA buffer is michael@0: * down-sampled to the associated GrTexture (accessible via michael@0: * GrRenderTarget::asTexture()). Any pending draws to the render target will michael@0: * be executed before the resolve. michael@0: * michael@0: * This is only necessary when a client wants to access the object directly michael@0: * using the backend API directly. GrContext will detect when it must michael@0: * perform a resolve to a GrTexture used as the source of a draw or before michael@0: * reading pixels back from a GrTexture or GrRenderTarget. michael@0: */ michael@0: void resolveRenderTarget(GrRenderTarget* target); michael@0: michael@0: #ifdef SK_DEVELOPER michael@0: void dumpFontCache() const; michael@0: #endif michael@0: michael@0: /////////////////////////////////////////////////////////////////////////// michael@0: // Helpers michael@0: michael@0: class AutoRenderTarget : public ::SkNoncopyable { michael@0: public: michael@0: AutoRenderTarget(GrContext* context, GrRenderTarget* target) { michael@0: fPrevTarget = context->getRenderTarget(); michael@0: SkSafeRef(fPrevTarget); michael@0: context->setRenderTarget(target); michael@0: fContext = context; michael@0: } michael@0: AutoRenderTarget(GrContext* context) { michael@0: fPrevTarget = context->getRenderTarget(); michael@0: SkSafeRef(fPrevTarget); michael@0: fContext = context; michael@0: } michael@0: ~AutoRenderTarget() { michael@0: if (NULL != fContext) { michael@0: fContext->setRenderTarget(fPrevTarget); michael@0: } michael@0: SkSafeUnref(fPrevTarget); michael@0: } michael@0: private: michael@0: GrContext* fContext; michael@0: GrRenderTarget* fPrevTarget; michael@0: }; michael@0: michael@0: /** michael@0: * Save/restore the view-matrix in the context. It can optionally adjust a paint to account michael@0: * for a coordinate system change. Here is an example of how the paint param can be used: michael@0: * michael@0: * A GrPaint is setup with GrEffects. The stages will have access to the pre-matrix source michael@0: * geometry positions when the draw is executed. Later on a decision is made to transform the michael@0: * geometry to device space on the CPU. The effects now need to know that the space in which michael@0: * the geometry will be specified has changed. michael@0: * michael@0: * Note that when restore is called (or in the destructor) the context's matrix will be michael@0: * restored. However, the paint will not be restored. The caller must make a copy of the michael@0: * paint if necessary. Hint: use SkTCopyOnFirstWrite if the AutoMatrix is conditionally michael@0: * initialized. michael@0: */ michael@0: class AutoMatrix : public ::SkNoncopyable { michael@0: public: michael@0: AutoMatrix() : fContext(NULL) {} michael@0: michael@0: ~AutoMatrix() { this->restore(); } michael@0: michael@0: /** michael@0: * Initializes by pre-concat'ing the context's current matrix with the preConcat param. michael@0: */ michael@0: void setPreConcat(GrContext* context, const SkMatrix& preConcat, GrPaint* paint = NULL) { michael@0: SkASSERT(NULL != context); michael@0: michael@0: this->restore(); michael@0: michael@0: fContext = context; michael@0: fMatrix = context->getMatrix(); michael@0: this->preConcat(preConcat, paint); michael@0: } michael@0: michael@0: /** michael@0: * Sets the context's matrix to identity. Returns false if the inverse matrix is required to michael@0: * update a paint but the matrix cannot be inverted. michael@0: */ michael@0: bool setIdentity(GrContext* context, GrPaint* paint = NULL) { michael@0: SkASSERT(NULL != context); michael@0: michael@0: this->restore(); michael@0: michael@0: if (NULL != paint) { michael@0: if (!paint->localCoordChangeInverse(context->getMatrix())) { michael@0: return false; michael@0: } michael@0: } michael@0: fMatrix = context->getMatrix(); michael@0: fContext = context; michael@0: context->setIdentityMatrix(); michael@0: return true; michael@0: } michael@0: michael@0: /** michael@0: * Replaces the context's matrix with a new matrix. Returns false if the inverse matrix is michael@0: * required to update a paint but the matrix cannot be inverted. michael@0: */ michael@0: bool set(GrContext* context, const SkMatrix& newMatrix, GrPaint* paint = NULL) { michael@0: if (NULL != paint) { michael@0: if (!this->setIdentity(context, paint)) { michael@0: return false; michael@0: } michael@0: this->preConcat(newMatrix, paint); michael@0: } else { michael@0: this->restore(); michael@0: fContext = context; michael@0: fMatrix = context->getMatrix(); michael@0: context->setMatrix(newMatrix); michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: /** michael@0: * If this has been initialized then the context's matrix will be further updated by michael@0: * pre-concat'ing the preConcat param. The matrix that will be restored remains unchanged. michael@0: * The paint is assumed to be relative to the context's matrix at the time this call is michael@0: * made, not the matrix at the time AutoMatrix was first initialized. In other words, this michael@0: * performs an incremental update of the paint. michael@0: */ michael@0: void preConcat(const SkMatrix& preConcat, GrPaint* paint = NULL) { michael@0: if (NULL != paint) { michael@0: paint->localCoordChange(preConcat); michael@0: } michael@0: fContext->concatMatrix(preConcat); michael@0: } michael@0: michael@0: /** michael@0: * Returns false if never initialized or the inverse matrix was required to update a paint michael@0: * but the matrix could not be inverted. michael@0: */ michael@0: bool succeeded() const { return NULL != fContext; } michael@0: michael@0: /** michael@0: * If this has been initialized then the context's original matrix is restored. michael@0: */ michael@0: void restore() { michael@0: if (NULL != fContext) { michael@0: fContext->setMatrix(fMatrix); michael@0: fContext = NULL; michael@0: } michael@0: } michael@0: michael@0: private: michael@0: GrContext* fContext; michael@0: SkMatrix fMatrix; michael@0: }; michael@0: michael@0: class AutoClip : public ::SkNoncopyable { michael@0: public: michael@0: // This enum exists to require a caller of the constructor to acknowledge that the clip will michael@0: // initially be wide open. It also could be extended if there are other desirable initial michael@0: // clip states. michael@0: enum InitialClip { michael@0: kWideOpen_InitialClip, michael@0: }; michael@0: michael@0: AutoClip(GrContext* context, InitialClip initialState) michael@0: : fContext(context) { michael@0: SkASSERT(kWideOpen_InitialClip == initialState); michael@0: fNewClipData.fClipStack = &fNewClipStack; michael@0: michael@0: fOldClip = context->getClip(); michael@0: context->setClip(&fNewClipData); michael@0: } michael@0: michael@0: AutoClip(GrContext* context, const SkRect& newClipRect) michael@0: : fContext(context) michael@0: , fNewClipStack(newClipRect) { michael@0: fNewClipData.fClipStack = &fNewClipStack; michael@0: michael@0: fOldClip = fContext->getClip(); michael@0: fContext->setClip(&fNewClipData); michael@0: } michael@0: michael@0: ~AutoClip() { michael@0: if (NULL != fContext) { michael@0: fContext->setClip(fOldClip); michael@0: } michael@0: } michael@0: private: michael@0: GrContext* fContext; michael@0: const GrClipData* fOldClip; michael@0: michael@0: SkClipStack fNewClipStack; michael@0: GrClipData fNewClipData; michael@0: }; michael@0: michael@0: class AutoWideOpenIdentityDraw { michael@0: public: michael@0: AutoWideOpenIdentityDraw(GrContext* ctx, GrRenderTarget* rt) michael@0: : fAutoClip(ctx, AutoClip::kWideOpen_InitialClip) michael@0: , fAutoRT(ctx, rt) { michael@0: fAutoMatrix.setIdentity(ctx); michael@0: // should never fail with no paint param. michael@0: SkASSERT(fAutoMatrix.succeeded()); michael@0: } michael@0: michael@0: private: michael@0: AutoClip fAutoClip; michael@0: AutoRenderTarget fAutoRT; michael@0: AutoMatrix fAutoMatrix; michael@0: }; michael@0: michael@0: /////////////////////////////////////////////////////////////////////////// michael@0: // Functions intended for internal use only. michael@0: GrGpu* getGpu() { return fGpu; } michael@0: const GrGpu* getGpu() const { return fGpu; } michael@0: GrFontCache* getFontCache() { return fFontCache; } michael@0: GrDrawTarget* getTextTarget(); michael@0: const GrIndexBuffer* getQuadIndexBuffer() const; michael@0: michael@0: // Called by tests that draw directly to the context via GrDrawTarget michael@0: void getTestTarget(GrTestTarget*); michael@0: michael@0: /** michael@0: * Stencil buffers add themselves to the cache using addStencilBuffer. findStencilBuffer is michael@0: * called to check the cache for a SB that matches an RT's criteria. michael@0: */ michael@0: void addStencilBuffer(GrStencilBuffer* sb); michael@0: GrStencilBuffer* findStencilBuffer(int width, int height, int sampleCnt); michael@0: michael@0: GrPathRenderer* getPathRenderer( michael@0: const SkPath& path, michael@0: const SkStrokeRec& stroke, michael@0: const GrDrawTarget* target, michael@0: bool allowSW, michael@0: GrPathRendererChain::DrawType drawType = GrPathRendererChain::kColor_DrawType, michael@0: GrPathRendererChain::StencilSupport* stencilSupport = NULL); michael@0: michael@0: michael@0: #if GR_CACHE_STATS michael@0: void printCacheStats() const; michael@0: #endif michael@0: michael@0: private: michael@0: // Used to indicate whether a draw should be performed immediately or queued in fDrawBuffer. michael@0: enum BufferedDraw { michael@0: kYes_BufferedDraw, michael@0: kNo_BufferedDraw, michael@0: }; michael@0: BufferedDraw fLastDrawWasBuffered; michael@0: michael@0: GrGpu* fGpu; michael@0: SkMatrix fViewMatrix; michael@0: SkAutoTUnref fRenderTarget; michael@0: const GrClipData* fClip; // TODO: make this ref counted michael@0: GrDrawState* fDrawState; michael@0: michael@0: GrResourceCache* fTextureCache; michael@0: GrFontCache* fFontCache; michael@0: michael@0: GrPathRendererChain* fPathRendererChain; michael@0: GrSoftwarePathRenderer* fSoftwarePathRenderer; michael@0: michael@0: GrVertexBufferAllocPool* fDrawBufferVBAllocPool; michael@0: GrIndexBufferAllocPool* fDrawBufferIBAllocPool; michael@0: GrInOrderDrawBuffer* fDrawBuffer; michael@0: michael@0: // Set by OverbudgetCB() to request that GrContext flush before exiting a draw. michael@0: bool fFlushToReduceCacheSize; michael@0: michael@0: GrAARectRenderer* fAARectRenderer; michael@0: GrOvalRenderer* fOvalRenderer; michael@0: michael@0: bool fDidTestPMConversions; michael@0: int fPMToUPMConversion; michael@0: int fUPMToPMConversion; michael@0: michael@0: struct CleanUpData { michael@0: PFCleanUpFunc fFunc; michael@0: void* fInfo; michael@0: }; michael@0: michael@0: SkTDArray fCleanUpData; michael@0: michael@0: int fMaxTextureSizeOverride; michael@0: michael@0: GrContext(); // init must be called after the constructor. michael@0: bool init(GrBackend, GrBackendContext); michael@0: michael@0: void setupDrawBuffer(); michael@0: michael@0: class AutoRestoreEffects; michael@0: class AutoCheckFlush; michael@0: /// Sets the paint and returns the target to draw into. The paint can be NULL in which case the michael@0: /// draw state is left unmodified. michael@0: GrDrawTarget* prepareToDraw(const GrPaint*, BufferedDraw, AutoRestoreEffects*, AutoCheckFlush*); michael@0: michael@0: void internalDrawPath(GrDrawTarget* target, bool useAA, const SkPath& path, michael@0: const SkStrokeRec& stroke); michael@0: michael@0: GrTexture* createResizedTexture(const GrTextureDesc& desc, michael@0: const GrCacheID& cacheID, michael@0: void* srcData, michael@0: size_t rowBytes, michael@0: bool filter); michael@0: michael@0: // Needed so GrTexture's returnToCache helper function can call michael@0: // addExistingTextureToCache michael@0: friend class GrTexture; michael@0: friend class GrStencilAndCoverPathRenderer; michael@0: michael@0: // Add an existing texture to the texture cache. This is intended solely michael@0: // for use with textures released from an GrAutoScratchTexture. michael@0: void addExistingTextureToCache(GrTexture* texture); michael@0: michael@0: /** michael@0: * These functions create premul <-> unpremul effects if it is possible to generate a pair michael@0: * of effects that make a readToUPM->writeToPM->readToUPM cycle invariant. Otherwise, they michael@0: * return NULL. michael@0: */ michael@0: const GrEffectRef* createPMToUPMEffect(GrTexture* texture, michael@0: bool swapRAndB, michael@0: const SkMatrix& matrix); michael@0: const GrEffectRef* createUPMToPMEffect(GrTexture* texture, michael@0: bool swapRAndB, michael@0: const SkMatrix& matrix); michael@0: michael@0: /** michael@0: * This callback allows the resource cache to callback into the GrContext michael@0: * when the cache is still overbudget after a purge. michael@0: */ michael@0: static bool OverbudgetCB(void* data); michael@0: michael@0: /** Creates a new gpu path, based on the specified path and stroke and returns it. michael@0: * The caller owns a ref on the returned path which must be balanced by a call to unref. michael@0: * michael@0: * @param skPath the path geometry. michael@0: * @param stroke the path stroke. michael@0: * @return a new path or NULL if the operation is not supported by the backend. michael@0: */ michael@0: GrPath* createPath(const SkPath& skPath, const SkStrokeRec& stroke); michael@0: michael@0: typedef SkRefCnt INHERITED; michael@0: }; michael@0: michael@0: /** michael@0: * Gets and locks a scratch texture from a descriptor using either exact or approximate criteria. michael@0: * Unlocks texture in the destructor. michael@0: */ michael@0: class GrAutoScratchTexture : public ::SkNoncopyable { michael@0: public: michael@0: GrAutoScratchTexture() michael@0: : fContext(NULL) michael@0: , fTexture(NULL) { michael@0: } michael@0: michael@0: GrAutoScratchTexture(GrContext* context, michael@0: const GrTextureDesc& desc, michael@0: GrContext::ScratchTexMatch match = GrContext::kApprox_ScratchTexMatch) michael@0: : fContext(NULL) michael@0: , fTexture(NULL) { michael@0: this->set(context, desc, match); michael@0: } michael@0: michael@0: ~GrAutoScratchTexture() { michael@0: this->reset(); michael@0: } michael@0: michael@0: void reset() { michael@0: if (NULL != fContext && NULL != fTexture) { michael@0: fContext->unlockScratchTexture(fTexture); michael@0: fTexture->unref(); michael@0: fTexture = NULL; michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * When detaching a texture we do not unlock it in the texture cache but michael@0: * we do set the returnToCache flag. In this way the texture remains michael@0: * "locked" in the texture cache until it is freed and recycled in michael@0: * GrTexture::internal_dispose. In reality, the texture has been removed michael@0: * from the cache (because this is in AutoScratchTexture) and by not michael@0: * calling unlockScratchTexture we simply don't re-add it. It will be michael@0: * reattached in GrTexture::internal_dispose. michael@0: * michael@0: * Note that the caller is assumed to accept and manage the ref to the michael@0: * returned texture. michael@0: */ michael@0: GrTexture* detach() { michael@0: if (NULL == fTexture) { michael@0: return NULL; michael@0: } michael@0: GrTexture* texture = fTexture; michael@0: fTexture = NULL; michael@0: michael@0: // This GrAutoScratchTexture has a ref from lockAndRefScratchTexture, which we give up now. michael@0: // The cache also has a ref which we are lending to the caller of detach(). When the caller michael@0: // lets go of the ref and the ref count goes to 0 internal_dispose will see this flag is michael@0: // set and re-ref the texture, thereby restoring the cache's ref. michael@0: SkASSERT(texture->getRefCnt() > 1); michael@0: texture->setFlag((GrTextureFlags) GrTexture::kReturnToCache_FlagBit); michael@0: texture->unref(); michael@0: SkASSERT(NULL != texture->getCacheEntry()); michael@0: michael@0: return texture; michael@0: } michael@0: michael@0: GrTexture* set(GrContext* context, michael@0: const GrTextureDesc& desc, michael@0: GrContext::ScratchTexMatch match = GrContext::kApprox_ScratchTexMatch) { michael@0: this->reset(); michael@0: michael@0: fContext = context; michael@0: if (NULL != fContext) { michael@0: fTexture = fContext->lockAndRefScratchTexture(desc, match); michael@0: if (NULL == fTexture) { michael@0: fContext = NULL; michael@0: } michael@0: return fTexture; michael@0: } else { michael@0: return NULL; michael@0: } michael@0: } michael@0: michael@0: GrTexture* texture() { return fTexture; } michael@0: michael@0: private: michael@0: GrContext* fContext; michael@0: GrTexture* fTexture; michael@0: }; michael@0: michael@0: #endif