1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/*
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2010 Google Inc.
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
627847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com */
727847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
827847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com#ifndef GrContext_DEFINED
927847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com#define GrContext_DEFINED
1027847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
11a2d71482db8b6d752a51c96da74768d7dfc27932robertphillips@google.com#include "GrClipData.h"
12a0b40280a49a8a43af7929ead3b3489951c58501commit-bot@chromium.org#include "GrColor.h"
13288d9549b42a4eb934e814790f2b7a81f017a9c5bsalomon@google.com#include "GrPaint.h"
1445a15f551b5b3c6c747d8eaf6466b7d3b76a8faebsalomon@google.com#include "GrPathRendererChain.h"
15fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com#include "GrRenderTarget.h"
16288d9549b42a4eb934e814790f2b7a81f017a9c5bsalomon@google.com#include "GrTexture.h"
17a0b40280a49a8a43af7929ead3b3489951c58501commit-bot@chromium.org#include "SkMatrix.h"
18e61c411c1258a323a010558c08de3d9f8d170dcaegdaniel#include "SkPathEffect.h"
19a0b40280a49a8a43af7929ead3b3489951c58501commit-bot@chromium.org#include "SkTypes.h"
2027847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
21bfe2b9d3a290d0153b82617cd6b65a4814fe89e3jvanverth@google.comclass GrAARectRenderer;
223b4dd90282932c9cd695d13f3876f98c9c6d6d5esenorblanco@chromium.orgclass GrAutoScratchTexture;
23089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.orgclass GrCacheable;
2410e04bf1c5025389d999f22467405782e9f9ffccbsalomon@google.comclass GrDrawState;
25583a1e38503ebd57ba9bd39a3fabe89bead8e76cbsalomon@google.comclass GrDrawTarget;
26a469c28c3c16214733a25201a286970f57b3d944bsalomon@google.comclass GrEffect;
2727847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.comclass GrFontCache;
2805ef510389950e1ae8dcba40e41e001db771b12dbsalomon@google.comclass GrGpu;
29bbcb38df4f7477aa0a38da00a0757586a0125954egdanielclass GrGpuTraceMarker;
30583a1e38503ebd57ba9bd39a3fabe89bead8e76cbsalomon@google.comclass GrIndexBuffer;
3127847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.comclass GrIndexBufferAllocPool;
3227847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.comclass GrInOrderDrawBuffer;
33e930a075625d3d90c023c2570a6a4cae1a4b484frobertphillips@google.comclass GrLayerCache;
3481312830ef73420efdc4821feb7c2d6fd9152af8commit-bot@chromium.orgclass GrOvalRenderer;
355c8ee2539b9316b22416a991a1f560ef5cec7957commit-bot@chromium.orgclass GrPath;
36583a1e38503ebd57ba9bd39a3fabe89bead8e76cbsalomon@google.comclass GrPathRenderer;
3750398bf7f1953e640e5529616e710cf540799731bsalomon@google.comclass GrResourceEntry;
3850398bf7f1953e640e5529616e710cf540799731bsalomon@google.comclass GrResourceCache;
39558a75bcb323c03dd7482555c7214062a363276fbsalomon@google.comclass GrStencilBuffer;
4078a1078f17f4f0ae63415298517262a64f706af6commit-bot@chromium.orgclass GrTestTarget;
41288d9549b42a4eb934e814790f2b7a81f017a9c5bsalomon@google.comclass GrTextureParams;
42583a1e38503ebd57ba9bd39a3fabe89bead8e76cbsalomon@google.comclass GrVertexBuffer;
4350398bf7f1953e640e5529616e710cf540799731bsalomon@google.comclass GrVertexBufferAllocPool;
44d58a0ba9cff9fcefe5047e88ccb4a6e76c591c40egdanielclass GrStrokeInfo;
4572176b2d38db005863a54e3dd6657bbabd068bb6robertphillips@google.comclass GrSoftwarePathRenderer;
465f74cf8c49701f514b69dc6f1a8b5c0ffd78af0asugoi@google.comclass SkStrokeRec;
4750398bf7f1953e640e5529616e710cf540799731bsalomon@google.com
48a4de8c257ea0be8ff7081f645249b6afe5c48e7ecommit-bot@chromium.orgclass SK_API GrContext : public SkRefCnt {
4927847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.compublic:
50fa35e3ddcc9d130ce87c927218bdf27879c38711reed@google.com    SK_DECLARE_INST_COUNT(GrContext)
51fa35e3ddcc9d130ce87c927218bdf27879c38711reed@google.com
5227847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    /**
5316e3ddea6a80972aced04b21b1d66377fa95e7c7bsalomon@google.com     * Creates a GrContext for a backend context.
5427847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     */
5516e3ddea6a80972aced04b21b1d66377fa95e7c7bsalomon@google.com    static GrContext* Create(GrBackend, GrBackendContext);
5627847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
5727847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    virtual ~GrContext();
5827847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
5927847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    /**
6027847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     * The GrContext normally assumes that no outsider is setting state
6127847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     * within the underlying 3D API's context/device/whatever. This call informs
6227847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     * the context that the state was modified and it should resend. Shouldn't
6327847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     * be called frequently for good performance.
640a208a117b2d7f2c2231aa357f1db4864dbdcba3bsalomon@google.com     * The flag bits, state, is dpendent on which backend is used by the
650a208a117b2d7f2c2231aa357f1db4864dbdcba3bsalomon@google.com     * context, either GL or D3D (possible in future).
6627847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     */
670a208a117b2d7f2c2231aa357f1db4864dbdcba3bsalomon@google.com    void resetContext(uint32_t state = kAll_GrBackendState);
6827847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
698fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com    /**
70cdb426d55a4bc4dae4f6d4f23e6994762950fdc3robertphillips@google.com     * Callback function to allow classes to cleanup on GrContext destruction.
71cdb426d55a4bc4dae4f6d4f23e6994762950fdc3robertphillips@google.com     * The 'info' field is filled in with the 'info' passed to addCleanUp.
72cdb426d55a4bc4dae4f6d4f23e6994762950fdc3robertphillips@google.com     */
73cdb426d55a4bc4dae4f6d4f23e6994762950fdc3robertphillips@google.com    typedef void (*PFCleanUpFunc)(const GrContext* context, void* info);
74cdb426d55a4bc4dae4f6d4f23e6994762950fdc3robertphillips@google.com
75cdb426d55a4bc4dae4f6d4f23e6994762950fdc3robertphillips@google.com    /**
76cdb426d55a4bc4dae4f6d4f23e6994762950fdc3robertphillips@google.com     * Add a function to be called from within GrContext's destructor.
77cdb426d55a4bc4dae4f6d4f23e6994762950fdc3robertphillips@google.com     * This gives classes a chance to free resources held on a per context basis.
78cdb426d55a4bc4dae4f6d4f23e6994762950fdc3robertphillips@google.com     * The 'info' parameter will be stored and passed to the callback function.
79cdb426d55a4bc4dae4f6d4f23e6994762950fdc3robertphillips@google.com     */
80cdb426d55a4bc4dae4f6d4f23e6994762950fdc3robertphillips@google.com    void addCleanUp(PFCleanUpFunc cleanUp, void* info) {
81cdb426d55a4bc4dae4f6d4f23e6994762950fdc3robertphillips@google.com        CleanUpData* entry = fCleanUpData.push();
82cdb426d55a4bc4dae4f6d4f23e6994762950fdc3robertphillips@google.com
83cdb426d55a4bc4dae4f6d4f23e6994762950fdc3robertphillips@google.com        entry->fFunc = cleanUp;
84cdb426d55a4bc4dae4f6d4f23e6994762950fdc3robertphillips@google.com        entry->fInfo = info;
85cdb426d55a4bc4dae4f6d4f23e6994762950fdc3robertphillips@google.com    }
86cdb426d55a4bc4dae4f6d4f23e6994762950fdc3robertphillips@google.com
87cdb426d55a4bc4dae4f6d4f23e6994762950fdc3robertphillips@google.com    /**
881e269b5a08610da13c3aee23809bb45b17e7b663bsalomon@google.com     * Abandons all GPU resources, assumes 3D API state is unknown. Call this
898fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     * if you have lost the associated GPU context, and thus internal texture,
908fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     * buffer, etc. references/IDs are now invalid. Should be called even when
918fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     * GrContext is no longer going to be used for two reasons:
928fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     *  1) ~GrContext will not try to free the objects in the 3D API.
93089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org     *  2) If you've created GrGpuObjects that outlive the GrContext they will
94089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org     *     be marked as invalid (GrGpuObjects::isValid()) and won't attempt to
958fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     *     free their underlying resource in the 3D API.
968fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     * Content drawn since the last GrContext::flush() may be lost.
978fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     */
988fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com    void contextLost();
9927847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
10027847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    /**
10153a5584bb0094e19c39a66cec7f346aad04ecac4junov@google.com     * Similar to contextLost, but makes no attempt to reset state.
10253a5584bb0094e19c39a66cec7f346aad04ecac4junov@google.com     * Use this method when GrContext destruction is pending, but
10353a5584bb0094e19c39a66cec7f346aad04ecac4junov@google.com     * the graphics context is destroyed first.
10453a5584bb0094e19c39a66cec7f346aad04ecac4junov@google.com     */
10553a5584bb0094e19c39a66cec7f346aad04ecac4junov@google.com    void contextDestroyed();
10653a5584bb0094e19c39a66cec7f346aad04ecac4junov@google.com
10795c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org    ///////////////////////////////////////////////////////////////////////////
10895c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org    // Resource Cache
10995c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org
11095c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org    /**
11195c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org     *  Return the current GPU resource cache limits.
11295c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org     *
11395c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org     *  @param maxResources If non-null, returns maximum number of resources that
11495c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org     *                      can be held in the cache.
11595c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org     *  @param maxResourceBytes If non-null, returns maximum number of bytes of
11695c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org     *                          video memory that can be held in the cache.
11795c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org     */
11895c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org    void getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const;
11995c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org    SK_ATTR_DEPRECATED("This function has been renamed to getResourceCacheLimits().")
12095c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org    void getTextureCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
12195c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org        this->getResourceCacheLimits(maxTextures, maxTextureBytes);
12295c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org    }
12395c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org
12495c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org    /**
12595c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org     *  Gets the current GPU resource cache usage.
12695c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org     *
12795c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org     *  @param resourceCount If non-null, returns the number of resources that are held in the
12895c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org     *                       cache.
12995c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org     *  @param maxResourceBytes If non-null, returns the total number of bytes of video memory held
13095c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org     *                          in the cache.
13195c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org     */
13295c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org    void getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const;
13395c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org
13495c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org    SK_ATTR_DEPRECATED("Use getResourceCacheUsage().")
13595c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org    size_t getGpuTextureCacheBytes() const {
13695c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org        size_t bytes;
13795c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org        this->getResourceCacheUsage(NULL, &bytes);
13895c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org        return bytes;
13995c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org    }
14095c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org
14195c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org    SK_ATTR_DEPRECATED("Use getResourceCacheUsage().")
14295c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org    int getGpuTextureCacheResourceCount() const {
14395c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org        int count;
14495c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org        this->getResourceCacheUsage(&count, NULL);
14595c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org        return count;
14695c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org    }
14795c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org
14895c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org    /**
14995c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org     *  Specify the GPU resource cache limits. If the current cache exceeds either
15095c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org     *  of these, it will be purged (LRU) to keep the cache within these limits.
15195c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org     *
15295c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org     *  @param maxResources The maximum number of resources that can be held in
15395c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org     *                      the cache.
15495c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org     *  @param maxResourceBytes The maximum number of bytes of video memory
15595c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org     *                          that can be held in the cache.
15695c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org     */
15795c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org    void setResourceCacheLimits(int maxResources, size_t maxResourceBytes);
15895c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org    SK_ATTR_DEPRECATED("This function has been renamed to setResourceCacheLimits().")
15995c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org    void setTextureCacheLimits(int maxTextures, size_t maxTextureBytes) {
16095c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org        this->setResourceCacheLimits(maxTextures, maxTextureBytes);
16195c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org    }
16295c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org
16353a5584bb0094e19c39a66cec7f346aad04ecac4junov@google.com    /**
1641e269b5a08610da13c3aee23809bb45b17e7b663bsalomon@google.com     * Frees GPU created by the context. Can be called to reduce GPU memory
1658fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     * pressure.
16627847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     */
1678fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com    void freeGpuResources();
1688fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
16907fc0d178e20f74a88dd78384f817b53204e625fbsalomon@google.com    /**
17095c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org     * This method should be called whenever a GrResource is unreffed or
17195c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org     * switched from exclusive to non-exclusive. This
17295c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org     * gives the resource cache a chance to discard unneeded resources.
17395c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org     * Note: this entry point will be removed once totally ref-driven
17495c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org     * cache maintenance is implemented.
17507fc0d178e20f74a88dd78384f817b53204e625fbsalomon@google.com     */
17695c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org    void purgeCache();
17795c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org
17895c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org    /**
17995c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org     * Purge all the unlocked resources from the cache.
18095c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org     * This entry point is mainly meant for timing texture uploads
18195c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org     * and is not defined in normal builds of Skia.
18295c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org     */
18395c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org    void purgeAllUnlockedResources();
18495c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org
18595c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org    /**
18695c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org     * Stores a custom resource in the cache, based on the specified key.
18795c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org     */
18895c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org    void addResourceToCache(const GrResourceKey&, GrCacheable*);
18907fc0d178e20f74a88dd78384f817b53204e625fbsalomon@google.com
190d8a57af725e8fa8905207df3cf7465be50598752commit-bot@chromium.org    /**
19195c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org     * Finds a resource in the cache, based on the specified key. This is intended for use in
19295c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org     * conjunction with addResourceToCache(). The return value will be NULL if not found. The
19395c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org     * caller must balance with a call to unref().
194d8a57af725e8fa8905207df3cf7465be50598752commit-bot@chromium.org     */
19595c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org    GrCacheable* findAndRefCachedResource(const GrResourceKey&);
196d8a57af725e8fa8905207df3cf7465be50598752commit-bot@chromium.org
1978fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com    ///////////////////////////////////////////////////////////////////////////
1988fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com    // Textures
19927847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
20027847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    /**
20195ed55adc6b8f0cee063c2cf2e14782773b0087fbsalomon@google.com     * Creates a new entry, based on the specified key and texture and returns it. The caller owns a
20295ed55adc6b8f0cee063c2cf2e14782773b0087fbsalomon@google.com     * ref on the returned texture which must be balanced by a call to unref.
2031fadb20c50c2302565f73ae12057a6f5d22192c7bsalomon@google.com     *
2041e269b5a08610da13c3aee23809bb45b17e7b663bsalomon@google.com     * @param params    The texture params used to draw a texture may help determine
205b8670998a59d305cd22a3c0cbdc6e075b0a37a6ebsalomon@google.com     *                  the cache entry used. (e.g. different versions may exist
206b8670998a59d305cd22a3c0cbdc6e075b0a37a6ebsalomon@google.com     *                  for different wrap modes on GPUs with limited NPOT
207b8670998a59d305cd22a3c0cbdc6e075b0a37a6ebsalomon@google.com     *                  texture support). NULL implies clamp wrap modes.
2081fadb20c50c2302565f73ae12057a6f5d22192c7bsalomon@google.com     * @param desc      Description of the texture properties.
2099c0e629c64c0fa93ac9bf5c2eaa1821370a6fbe5krajcevski     * @param cacheID   Cache-specific properties (e.g., texture gen ID)
2101fadb20c50c2302565f73ae12057a6f5d22192c7bsalomon@google.com     * @param srcData   Pointer to the pixel values.
2111fadb20c50c2302565f73ae12057a6f5d22192c7bsalomon@google.com     * @param rowBytes  The number of bytes between rows of the texture. Zero
2129c0e629c64c0fa93ac9bf5c2eaa1821370a6fbe5krajcevski     *                  implies tightly packed rows. For compressed pixel configs, this
2139c0e629c64c0fa93ac9bf5c2eaa1821370a6fbe5krajcevski     *                  field is ignored.
21450a3043194cf278a74ff51c33c6cdb52cbe1f8f9commit-bot@chromium.org     * @param cacheKey  (optional) If non-NULL, we'll write the cache key we used to cacheKey.
2151fadb20c50c2302565f73ae12057a6f5d22192c7bsalomon@google.com     */
2169fbcad0f00d7098574cf3394a812c9d845c9cc5brobertphillips@google.com    GrTexture* createTexture(const GrTextureParams* params,
2179fbcad0f00d7098574cf3394a812c9d845c9cc5brobertphillips@google.com                             const GrTextureDesc& desc,
2180797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com                             const GrCacheID& cacheID,
219f9bd04faffb936602f7f957413a8cd5b7fc44addcommit-bot@chromium.org                             const void* srcData,
22050a3043194cf278a74ff51c33c6cdb52cbe1f8f9commit-bot@chromium.org                             size_t rowBytes,
22150a3043194cf278a74ff51c33c6cdb52cbe1f8f9commit-bot@chromium.org                             GrResourceKey* cacheKey = NULL);
2221fadb20c50c2302565f73ae12057a6f5d22192c7bsalomon@google.com    /**
22395ed55adc6b8f0cee063c2cf2e14782773b0087fbsalomon@google.com     * Search for an entry based on key and dimensions. If found, ref it and return it. The return
22495ed55adc6b8f0cee063c2cf2e14782773b0087fbsalomon@google.com     * value will be NULL if not found. The caller must balance with a call to unref.
2251fadb20c50c2302565f73ae12057a6f5d22192c7bsalomon@google.com     *
226b8670998a59d305cd22a3c0cbdc6e075b0a37a6ebsalomon@google.com     *  @param desc     Description of the texture properties.
2270797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com     *  @param cacheID Cache-specific properties (e.g., texture gen ID)
2281e269b5a08610da13c3aee23809bb45b17e7b663bsalomon@google.com     *  @param params   The texture params used to draw a texture may help determine
229b8670998a59d305cd22a3c0cbdc6e075b0a37a6ebsalomon@google.com     *                  the cache entry used. (e.g. different versions may exist
230b8670998a59d305cd22a3c0cbdc6e075b0a37a6ebsalomon@google.com     *                  for different wrap modes on GPUs with limited NPOT
231b8670998a59d305cd22a3c0cbdc6e075b0a37a6ebsalomon@google.com     *                  texture support). NULL implies clamp wrap modes.
23250398bf7f1953e640e5529616e710cf540799731bsalomon@google.com     */
23395ed55adc6b8f0cee063c2cf2e14782773b0087fbsalomon@google.com    GrTexture* findAndRefTexture(const GrTextureDesc& desc,
23495ed55adc6b8f0cee063c2cf2e14782773b0087fbsalomon@google.com                                 const GrCacheID& cacheID,
23595ed55adc6b8f0cee063c2cf2e14782773b0087fbsalomon@google.com                                 const GrTextureParams* params);
236fb30951cd9346a7a2d36e7d5f81f9e7ee792b669bsalomon@google.com    /**
237fb30951cd9346a7a2d36e7d5f81f9e7ee792b669bsalomon@google.com     * Determines whether a texture is in the cache. If the texture is found it
238fb30951cd9346a7a2d36e7d5f81f9e7ee792b669bsalomon@google.com     * will not be locked or returned. This call does not affect the priority of
239fb30951cd9346a7a2d36e7d5f81f9e7ee792b669bsalomon@google.com     * the texture for deletion.
240fb30951cd9346a7a2d36e7d5f81f9e7ee792b669bsalomon@google.com     */
24175b3c9633cb9a594dab0ccf51dab1e694c149a18robertphillips@google.com    bool isTextureInCache(const GrTextureDesc& desc,
2420797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com                          const GrCacheID& cacheID,
243b8670998a59d305cd22a3c0cbdc6e075b0a37a6ebsalomon@google.com                          const GrTextureParams* params) const;
24450398bf7f1953e640e5529616e710cf540799731bsalomon@google.com
24550398bf7f1953e640e5529616e710cf540799731bsalomon@google.com    /**
24650398bf7f1953e640e5529616e710cf540799731bsalomon@google.com     * Enum that determines how closely a returned scratch texture must match
24750398bf7f1953e640e5529616e710cf540799731bsalomon@google.com     * a provided GrTextureDesc.
24850398bf7f1953e640e5529616e710cf540799731bsalomon@google.com     */
24950398bf7f1953e640e5529616e710cf540799731bsalomon@google.com    enum ScratchTexMatch {
25050398bf7f1953e640e5529616e710cf540799731bsalomon@google.com        /**
25150398bf7f1953e640e5529616e710cf540799731bsalomon@google.com         * Finds a texture that exactly matches the descriptor.
25250398bf7f1953e640e5529616e710cf540799731bsalomon@google.com         */
25350398bf7f1953e640e5529616e710cf540799731bsalomon@google.com        kExact_ScratchTexMatch,
25450398bf7f1953e640e5529616e710cf540799731bsalomon@google.com        /**
25550398bf7f1953e640e5529616e710cf540799731bsalomon@google.com         * Finds a texture that approximately matches the descriptor. Will be
25650398bf7f1953e640e5529616e710cf540799731bsalomon@google.com         * at least as large in width and height as desc specifies. If desc
25750398bf7f1953e640e5529616e710cf540799731bsalomon@google.com         * specifies that texture is a render target then result will be a
25850398bf7f1953e640e5529616e710cf540799731bsalomon@google.com         * render target. If desc specifies a render target and doesn't set the
25950398bf7f1953e640e5529616e710cf540799731bsalomon@google.com         * no stencil flag then result will have a stencil. Format and aa level
26050398bf7f1953e640e5529616e710cf540799731bsalomon@google.com         * will always match.
26150398bf7f1953e640e5529616e710cf540799731bsalomon@google.com         */
26250398bf7f1953e640e5529616e710cf540799731bsalomon@google.com        kApprox_ScratchTexMatch
26350398bf7f1953e640e5529616e710cf540799731bsalomon@google.com    };
26427847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
26527847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    /**
266fea37b5e532dfe776269253afb9951e763c3b205bsalomon@google.com     * Returns a texture matching the desc. It's contents are unknown. Subsequent
267fea37b5e532dfe776269253afb9951e763c3b205bsalomon@google.com     * requests with the same descriptor are not guaranteed to return the same
268fea37b5e532dfe776269253afb9951e763c3b205bsalomon@google.com     * texture. The same texture is guaranteed not be returned again until it is
26995ed55adc6b8f0cee063c2cf2e14782773b0087fbsalomon@google.com     * unlocked. Call must be balanced with an unlockTexture() call. The caller
27095ed55adc6b8f0cee063c2cf2e14782773b0087fbsalomon@google.com     * owns a ref on the returned texture and must balance with a call to unref.
271a39f404c87e9957947e3341e46056e4f589c91adbsalomon@google.com     *
272a39f404c87e9957947e3341e46056e4f589c91adbsalomon@google.com     * Textures created by createAndLockTexture() hide the complications of
273fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com     * tiling non-power-of-two textures on APIs that don't support this (e.g.
2741e269b5a08610da13c3aee23809bb45b17e7b663bsalomon@google.com     * unextended GLES2). Tiling a NPOT texture created by lockScratchTexture on
275a39f404c87e9957947e3341e46056e4f589c91adbsalomon@google.com     * such an API will create gaps in the tiling pattern. This includes clamp
276a39f404c87e9957947e3341e46056e4f589c91adbsalomon@google.com     * mode. (This may be addressed in a future update.)
27727847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     */
27895ed55adc6b8f0cee063c2cf2e14782773b0087fbsalomon@google.com    GrTexture* lockAndRefScratchTexture(const GrTextureDesc&, ScratchTexMatch match);
279b5b3168a645802f66233234a06dd5a3764f18018bsalomon@google.com
280b5b3168a645802f66233234a06dd5a3764f18018bsalomon@google.com    /**
2810797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com     *  When done with an entry, call unlockScratchTexture(entry) on it, which returns
28295ed55adc6b8f0cee063c2cf2e14782773b0087fbsalomon@google.com     *  it to the cache, where it may be purged. This does not unref the texture.
28327847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     */
2849fbcad0f00d7098574cf3394a812c9d845c9cc5brobertphillips@google.com    void unlockScratchTexture(GrTexture* texture);
28527847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
28627847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    /**
28727847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     * Creates a texture that is outside the cache. Does not count against
28827847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     * cache's budget.
28927847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     */
29075b3c9633cb9a594dab0ccf51dab1e694c149a18robertphillips@google.com    GrTexture* createUncachedTexture(const GrTextureDesc& desc,
29127847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com                                     void* srcData,
29227847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com                                     size_t rowBytes);
29327847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
29427847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    /**
295b8670998a59d305cd22a3c0cbdc6e075b0a37a6ebsalomon@google.com     * Returns true if the specified use of an indexed texture is supported.
296b8670998a59d305cd22a3c0cbdc6e075b0a37a6ebsalomon@google.com     * Support may depend upon whether the texture params indicate that the
297b8670998a59d305cd22a3c0cbdc6e075b0a37a6ebsalomon@google.com     * texture will be tiled. Passing NULL for the texture params indicates
298b8670998a59d305cd22a3c0cbdc6e075b0a37a6ebsalomon@google.com     * clamp mode.
29927847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     */
300b8670998a59d305cd22a3c0cbdc6e075b0a37a6ebsalomon@google.com    bool supportsIndex8PixelConfig(const GrTextureParams*,
3011f221a70214fa1ab87b8a32dd66facf485f318eebsalomon@google.com                                   int width,
3021f221a70214fa1ab87b8a32dd66facf485f318eebsalomon@google.com                                   int height) const;
30327847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
30427847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    /**
3051e269b5a08610da13c3aee23809bb45b17e7b663bsalomon@google.com     *  Return the max width or height of a texture supported by the current GPU.
30627847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     */
307919583674bd5daeb60327c0bc1ce8aaa80d54e13bsalomon@google.com    int getMaxTextureSize() const;
308919583674bd5daeb60327c0bc1ce8aaa80d54e13bsalomon@google.com
30944a91dcf2542c2aa93f159ebbb5bf813ae7362c0robertphillips@google.com    /**
310956b310f13c7412c035406c658ff16ca85eac656skia.committer@gmail.com     *  Temporarily override the true max texture size. Note: an override
31144a91dcf2542c2aa93f159ebbb5bf813ae7362c0robertphillips@google.com     *  larger then the true max texture size will have no effect.
31244a91dcf2542c2aa93f159ebbb5bf813ae7362c0robertphillips@google.com     *  This entry point is mainly meant for testing texture size dependent
31344a91dcf2542c2aa93f159ebbb5bf813ae7362c0robertphillips@google.com     *  features and is only available if defined outside of Skia (see
31444a91dcf2542c2aa93f159ebbb5bf813ae7362c0robertphillips@google.com     *  bleed GM.
31544a91dcf2542c2aa93f159ebbb5bf813ae7362c0robertphillips@google.com     */
31644a91dcf2542c2aa93f159ebbb5bf813ae7362c0robertphillips@google.com    void setMaxTextureSizeOverride(int maxTextureSizeOverride);
31744a91dcf2542c2aa93f159ebbb5bf813ae7362c0robertphillips@google.com
31827847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    ///////////////////////////////////////////////////////////////////////////
31927847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    // Render targets
32027847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
32127847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    /**
3225877ffd5ea71a3ea70096d5c11c843798defa690bsalomon@google.com     * Sets the render target.
323bb6a3178c3e79c8549b332e4ce84c64b59964f1ecommit-bot@chromium.org     * @param target    the render target to set.
3245877ffd5ea71a3ea70096d5c11c843798defa690bsalomon@google.com     */
325bb6a3178c3e79c8549b332e4ce84c64b59964f1ecommit-bot@chromium.org    void setRenderTarget(GrRenderTarget* target) {
326bb6a3178c3e79c8549b332e4ce84c64b59964f1ecommit-bot@chromium.org        fRenderTarget.reset(SkSafeRef(target));
327bb6a3178c3e79c8549b332e4ce84c64b59964f1ecommit-bot@chromium.org    }
3285877ffd5ea71a3ea70096d5c11c843798defa690bsalomon@google.com
3295877ffd5ea71a3ea70096d5c11c843798defa690bsalomon@google.com    /**
3305877ffd5ea71a3ea70096d5c11c843798defa690bsalomon@google.com     * Gets the current render target.
331bb6a3178c3e79c8549b332e4ce84c64b59964f1ecommit-bot@chromium.org     * @return the currently bound render target.
3325877ffd5ea71a3ea70096d5c11c843798defa690bsalomon@google.com     */
333bb6a3178c3e79c8549b332e4ce84c64b59964f1ecommit-bot@chromium.org    const GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); }
334bb6a3178c3e79c8549b332e4ce84c64b59964f1ecommit-bot@chromium.org    GrRenderTarget* getRenderTarget() { return fRenderTarget.get(); }
3355877ffd5ea71a3ea70096d5c11c843798defa690bsalomon@google.com
33699a5ac0b67a14048a1db3f429878775854d29397robertphillips@google.com    /**
33799a5ac0b67a14048a1db3f429878775854d29397robertphillips@google.com     * Can the provided configuration act as a color render target?
33899a5ac0b67a14048a1db3f429878775854d29397robertphillips@google.com     */
3396b7938f9cb250086a7c6325cf31576f894ca4a1bcommit-bot@chromium.org    bool isConfigRenderable(GrPixelConfig config, bool withMSAA) const;
34099a5ac0b67a14048a1db3f429878775854d29397robertphillips@google.com
3418a70eef71cf369803e97ffea786f43f944de758ebsalomon@google.com    /**
3428a70eef71cf369803e97ffea786f43f944de758ebsalomon@google.com     * Return the max width or height of a render target supported by the
3438a70eef71cf369803e97ffea786f43f944de758ebsalomon@google.com     * current GPU.
3448a70eef71cf369803e97ffea786f43f944de758ebsalomon@google.com     */
3458a70eef71cf369803e97ffea786f43f944de758ebsalomon@google.com    int getMaxRenderTargetSize() const;
3468a70eef71cf369803e97ffea786f43f944de758ebsalomon@google.com
3478a70eef71cf369803e97ffea786f43f944de758ebsalomon@google.com    /**
3488a70eef71cf369803e97ffea786f43f944de758ebsalomon@google.com     * Returns the max sample count for a render target. It will be 0 if MSAA
3498a70eef71cf369803e97ffea786f43f944de758ebsalomon@google.com     * is not supported.
3508a70eef71cf369803e97ffea786f43f944de758ebsalomon@google.com     */
3518a70eef71cf369803e97ffea786f43f944de758ebsalomon@google.com    int getMaxSampleCount() const;
3528a70eef71cf369803e97ffea786f43f944de758ebsalomon@google.com
353b471a32460a44043e1f00d28cbefc87579dc30c5commit-bot@chromium.org    /**
354b471a32460a44043e1f00d28cbefc87579dc30c5commit-bot@chromium.org     * Returns the recommended sample count for a render target when using this
355b471a32460a44043e1f00d28cbefc87579dc30c5commit-bot@chromium.org     * context.
356b471a32460a44043e1f00d28cbefc87579dc30c5commit-bot@chromium.org     *
357b471a32460a44043e1f00d28cbefc87579dc30c5commit-bot@chromium.org     * @param  config the configuration of the render target.
358b471a32460a44043e1f00d28cbefc87579dc30c5commit-bot@chromium.org     * @param  dpi the display density in dots per inch.
359b471a32460a44043e1f00d28cbefc87579dc30c5commit-bot@chromium.org     *
360b471a32460a44043e1f00d28cbefc87579dc30c5commit-bot@chromium.org     * @return sample count that should be perform well and have good enough
361b471a32460a44043e1f00d28cbefc87579dc30c5commit-bot@chromium.org     *         rendering quality for the display. Alternatively returns 0 if
362b471a32460a44043e1f00d28cbefc87579dc30c5commit-bot@chromium.org     *         MSAA is not supported or recommended to be used by default.
363b471a32460a44043e1f00d28cbefc87579dc30c5commit-bot@chromium.org     */
364b471a32460a44043e1f00d28cbefc87579dc30c5commit-bot@chromium.org    int getRecommendedSampleCount(GrPixelConfig config, SkScalar dpi) const;
365b471a32460a44043e1f00d28cbefc87579dc30c5commit-bot@chromium.org
3665877ffd5ea71a3ea70096d5c11c843798defa690bsalomon@google.com    ///////////////////////////////////////////////////////////////////////////
36716e3ddea6a80972aced04b21b1d66377fa95e7c7bsalomon@google.com    // Backend Surfaces
3685877ffd5ea71a3ea70096d5c11c843798defa690bsalomon@google.com
3695877ffd5ea71a3ea70096d5c11c843798defa690bsalomon@google.com    /**
370e269f210bdae0288643afaf8a579b22d3f6d5bebbsalomon@google.com     * Wraps an existing texture with a GrTexture object.
371e269f210bdae0288643afaf8a579b22d3f6d5bebbsalomon@google.com     *
372e269f210bdae0288643afaf8a579b22d3f6d5bebbsalomon@google.com     * OpenGL: if the object is a texture Gr may change its GL texture params
373e269f210bdae0288643afaf8a579b22d3f6d5bebbsalomon@google.com     *         when it is drawn.
374e269f210bdae0288643afaf8a579b22d3f6d5bebbsalomon@google.com     *
375e269f210bdae0288643afaf8a579b22d3f6d5bebbsalomon@google.com     * @param  desc     description of the object to create.
376e269f210bdae0288643afaf8a579b22d3f6d5bebbsalomon@google.com     *
377e269f210bdae0288643afaf8a579b22d3f6d5bebbsalomon@google.com     * @return GrTexture object or NULL on failure.
378e269f210bdae0288643afaf8a579b22d3f6d5bebbsalomon@google.com     */
37916e3ddea6a80972aced04b21b1d66377fa95e7c7bsalomon@google.com    GrTexture* wrapBackendTexture(const GrBackendTextureDesc& desc);
380e269f210bdae0288643afaf8a579b22d3f6d5bebbsalomon@google.com
381e269f210bdae0288643afaf8a579b22d3f6d5bebbsalomon@google.com    /**
382e269f210bdae0288643afaf8a579b22d3f6d5bebbsalomon@google.com     * Wraps an existing render target with a GrRenderTarget object. It is
38316e3ddea6a80972aced04b21b1d66377fa95e7c7bsalomon@google.com     * similar to wrapBackendTexture but can be used to draw into surfaces
384e269f210bdae0288643afaf8a579b22d3f6d5bebbsalomon@google.com     * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that
385e269f210bdae0288643afaf8a579b22d3f6d5bebbsalomon@google.com     * the client will resolve to a texture).
386e269f210bdae0288643afaf8a579b22d3f6d5bebbsalomon@google.com     *
387e269f210bdae0288643afaf8a579b22d3f6d5bebbsalomon@google.com     * @param  desc     description of the object to create.
388e269f210bdae0288643afaf8a579b22d3f6d5bebbsalomon@google.com     *
389e269f210bdae0288643afaf8a579b22d3f6d5bebbsalomon@google.com     * @return GrTexture object or NULL on failure.
390e269f210bdae0288643afaf8a579b22d3f6d5bebbsalomon@google.com     */
39116e3ddea6a80972aced04b21b1d66377fa95e7c7bsalomon@google.com     GrRenderTarget* wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc);
392e269f210bdae0288643afaf8a579b22d3f6d5bebbsalomon@google.com
39327847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    ///////////////////////////////////////////////////////////////////////////
39427847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    // Matrix state
39527847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
39627847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    /**
39727847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     * Gets the current transformation matrix.
39827847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     * @return the current matrix.
39927847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     */
400bb6a3178c3e79c8549b332e4ce84c64b59964f1ecommit-bot@chromium.org    const SkMatrix& getMatrix() const { return fViewMatrix; }
40127847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
40227847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    /**
40327847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     * Sets the transformation matrix.
40427847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     * @param m the matrix to set.
40527847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     */
406bb6a3178c3e79c8549b332e4ce84c64b59964f1ecommit-bot@chromium.org    void setMatrix(const SkMatrix& m) { fViewMatrix = m; }
40727847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
40827847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    /**
4090f11e1ab5b6e53f6176dde2dbb25a8e3ae34858fbsalomon@google.com     * Sets the current transformation matrix to identity.
4100f11e1ab5b6e53f6176dde2dbb25a8e3ae34858fbsalomon@google.com     */
411bb6a3178c3e79c8549b332e4ce84c64b59964f1ecommit-bot@chromium.org    void setIdentityMatrix() { fViewMatrix.reset(); }
4120f11e1ab5b6e53f6176dde2dbb25a8e3ae34858fbsalomon@google.com
4130f11e1ab5b6e53f6176dde2dbb25a8e3ae34858fbsalomon@google.com    /**
41427847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     * Concats the current matrix. The passed matrix is applied before the
41527847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     * current matrix.
41627847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     * @param m the matrix to concat.
41727847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     */
418bb6a3178c3e79c8549b332e4ce84c64b59964f1ecommit-bot@chromium.org    void concatMatrix(const SkMatrix& m) { fViewMatrix.preConcat(m); }
41927847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
42027847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
42127847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    ///////////////////////////////////////////////////////////////////////////
42227847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    // Clip state
42327847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    /**
42427847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     * Gets the current clip.
42527847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     * @return the current clip.
42627847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     */
427bb6a3178c3e79c8549b332e4ce84c64b59964f1ecommit-bot@chromium.org    const GrClipData* getClip() const { return fClip; }
42827847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
42927847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    /**
43027847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     * Sets the clip.
431beb1af78d016d2700c350487a383c6bcfa7e2e20robertphillips@google.com     * @param clipData  the clip to set.
43227847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     */
433bb6a3178c3e79c8549b332e4ce84c64b59964f1ecommit-bot@chromium.org    void setClip(const GrClipData* clipData) { fClip = clipData; }
43427847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
43527847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    ///////////////////////////////////////////////////////////////////////////
43627847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    // Draws
43727847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
43827847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    /**
4396aa25c3f555dc2a6711365d14279db3ec909e064bsalomon@google.com     * Clear the entire or rect of the render target, ignoring any clips.
4406aa25c3f555dc2a6711365d14279db3ec909e064bsalomon@google.com     * @param rect  the rect to clear or the whole thing if rect is NULL.
4416aa25c3f555dc2a6711365d14279db3ec909e064bsalomon@google.com     * @param color the color to clear to.
44256ce48ade325f6f49acb0da31d6252806e4ed7efrobertphillips@google.com     * @param canIgnoreRect allows partial clears to be converted to whole
44356ce48ade325f6f49acb0da31d6252806e4ed7efrobertphillips@google.com     *                      clears on platforms for which that is cheap
444c82a8b7aa4ec19fba508c394920a9e88d3e5bd12robertphillips@google.com     * @param target if non-NULL, the render target to clear otherwise clear
445c82a8b7aa4ec19fba508c394920a9e88d3e5bd12robertphillips@google.com     *               the current render target
44627847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     */
44756ce48ade325f6f49acb0da31d6252806e4ed7efrobertphillips@google.com    void clear(const SkIRect* rect, GrColor color, bool canIgnoreRect,
448c82a8b7aa4ec19fba508c394920a9e88d3e5bd12robertphillips@google.com               GrRenderTarget* target = NULL);
44927847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
45027847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    /**
45127847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     *  Draw everywhere (respecting the clip) with the paint.
45227847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     */
45327847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    void drawPaint(const GrPaint& paint);
45427847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
45527847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    /**
45627847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     *  Draw the rect using a paint.
45727847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     *  @param paint        describes how to color pixels.
458d58a0ba9cff9fcefe5047e88ccb4a6e76c591c40egdaniel     *  @param strokeInfo   the stroke information (width, join, cap), and.
459d58a0ba9cff9fcefe5047e88ccb4a6e76c591c40egdaniel     *                      the dash information (intervals, count, phase).
460d58a0ba9cff9fcefe5047e88ccb4a6e76c591c40egdaniel     *                      If strokeInfo == NULL, then the rect is filled.
4616006d0f8c4f19d19a12de20826f731f52ac822a7commit-bot@chromium.org     *                      Otherwise, if stroke width == 0, then the stroke
4622614418aee9f12e07a4483e32f72ee5393ed12baskia.committer@gmail.com     *                      is always a single pixel thick, else the rect is
4636006d0f8c4f19d19a12de20826f731f52ac822a7commit-bot@chromium.org     *                      mitered/beveled stroked based on stroke width.
464d58a0ba9cff9fcefe5047e88ccb4a6e76c591c40egdaniel     *                      If the stroke is dashed the rect is sent to drawPath.
46527847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     *  @param matrix       Optional matrix applied to the rect. Applied before
46627847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     *                      context's matrix or the paint's matrix.
46727847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     *  The rects coords are used to access the paint (through texture matrix)
46827847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     */
46927847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    void drawRect(const GrPaint& paint,
470fd03d4a829efe2d77a712fd991927c55f59a2ffecommit-bot@chromium.org                  const SkRect&,
471d58a0ba9cff9fcefe5047e88ccb4a6e76c591c40egdaniel                  const GrStrokeInfo* strokeInfo = NULL,
472b9086a026844e4cfd08b219e49ce3f12294cba98bsalomon@google.com                  const SkMatrix* matrix = NULL);
47327847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
47427847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    /**
475c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com     * Maps a rect of local coordinates onto the a rect of destination
476c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com     * coordinates. Each rect can optionally be transformed. The localRect
47727847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     * is stretched over the dstRect. The dstRect is transformed by the
478c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com     * context's matrix. Additional optional matrices for both rects can be
479c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com     * provided by parameters.
48027847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     *
481c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com     * @param paint         describes how to color pixels.
482c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com     * @param dstRect       the destination rect to draw.
483c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com     * @param localRect     rect of local coordinates to be mapped onto dstRect
484c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com     * @param dstMatrix     Optional matrix to transform dstRect. Applied before context's matrix.
485c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com     * @param localMatrix   Optional matrix to transform localRect.
48627847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     */
48727847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    void drawRectToRect(const GrPaint& paint,
488fd03d4a829efe2d77a712fd991927c55f59a2ffecommit-bot@chromium.org                        const SkRect& dstRect,
489fd03d4a829efe2d77a712fd991927c55f59a2ffecommit-bot@chromium.org                        const SkRect& localRect,
490b9086a026844e4cfd08b219e49ce3f12294cba98bsalomon@google.com                        const SkMatrix* dstMatrix = NULL,
491c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com                        const SkMatrix* localMatrix = NULL);
49227847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
49327847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    /**
494f2bfd54de32ffbcf90ddcd0e249aaebb1559d9c2commit-bot@chromium.org     *  Draw a roundrect using a paint.
495f2bfd54de32ffbcf90ddcd0e249aaebb1559d9c2commit-bot@chromium.org     *
496f2bfd54de32ffbcf90ddcd0e249aaebb1559d9c2commit-bot@chromium.org     *  @param paint        describes how to color pixels.
497f2bfd54de32ffbcf90ddcd0e249aaebb1559d9c2commit-bot@chromium.org     *  @param rrect        the roundrect to draw
498d58a0ba9cff9fcefe5047e88ccb4a6e76c591c40egdaniel     *  @param strokeInfo   the stroke information (width, join, cap) and
499d58a0ba9cff9fcefe5047e88ccb4a6e76c591c40egdaniel     *                      the dash information (intervals, count, phase).
500f2bfd54de32ffbcf90ddcd0e249aaebb1559d9c2commit-bot@chromium.org     */
501d58a0ba9cff9fcefe5047e88ccb4a6e76c591c40egdaniel    void drawRRect(const GrPaint& paint, const SkRRect& rrect, const GrStrokeInfo& strokeInfo);
5020a09d7195b8d9945e5c9c76cc4cfe6ef65d6d390commit-bot@chromium.org
5030a09d7195b8d9945e5c9c76cc4cfe6ef65d6d390commit-bot@chromium.org    /**
5040a09d7195b8d9945e5c9c76cc4cfe6ef65d6d390commit-bot@chromium.org     *  Shortcut for drawing an SkPath consisting of nested rrects using a paint.
5050a09d7195b8d9945e5c9c76cc4cfe6ef65d6d390commit-bot@chromium.org     *  Does not support stroking. The result is undefined if outer does not contain
5060a09d7195b8d9945e5c9c76cc4cfe6ef65d6d390commit-bot@chromium.org     *  inner.
5070a09d7195b8d9945e5c9c76cc4cfe6ef65d6d390commit-bot@chromium.org     *
5080a09d7195b8d9945e5c9c76cc4cfe6ef65d6d390commit-bot@chromium.org     *  @param paint        describes how to color pixels.
5090a09d7195b8d9945e5c9c76cc4cfe6ef65d6d390commit-bot@chromium.org     *  @param outer        the outer roundrect
5100a09d7195b8d9945e5c9c76cc4cfe6ef65d6d390commit-bot@chromium.org     *  @param inner        the inner roundrect
5110a09d7195b8d9945e5c9c76cc4cfe6ef65d6d390commit-bot@chromium.org     */
5120a09d7195b8d9945e5c9c76cc4cfe6ef65d6d390commit-bot@chromium.org    void drawDRRect(const GrPaint& paint, const SkRRect& outer, const SkRRect& inner);
5130a09d7195b8d9945e5c9c76cc4cfe6ef65d6d390commit-bot@chromium.org
514f2bfd54de32ffbcf90ddcd0e249aaebb1559d9c2commit-bot@chromium.org
515f2bfd54de32ffbcf90ddcd0e249aaebb1559d9c2commit-bot@chromium.org    /**
516d302f1401b3c9aea094804bad4e76de98782cfe8bsalomon@google.com     * Draws a path.
51727847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     *
51827847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     * @param paint         describes how to color pixels.
51907f3ee10d34f09342abb93d758b5e151ff78f7a5reed@google.com     * @param path          the path to draw
520d58a0ba9cff9fcefe5047e88ccb4a6e76c591c40egdaniel     * @param strokeInfo    the stroke information (width, join, cap) and
521d58a0ba9cff9fcefe5047e88ccb4a6e76c591c40egdaniel     *                      the dash information (intervals, count, phase).
52227847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     */
523d58a0ba9cff9fcefe5047e88ccb4a6e76c591c40egdaniel    void drawPath(const GrPaint& paint, const SkPath& path, const GrStrokeInfo& strokeInfo);
52407f3ee10d34f09342abb93d758b5e151ff78f7a5reed@google.com
52527847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    /**
52627847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     * Draws vertices with a paint.
52727847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     *
52827847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     * @param   paint           describes how to color pixels.
52927847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     * @param   primitiveType   primitives type to draw.
53027847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     * @param   vertexCount     number of vertices.
53127847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     * @param   positions       array of vertex positions, required.
53227847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     * @param   texCoords       optional array of texture coordinates used
53327847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     *                          to access the paint.
53427847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     * @param   colors          optional array of per-vertex colors, supercedes
53527847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     *                          the paint's color field.
53627847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     * @param   indices         optional array of indices. If NULL vertices
53727847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     *                          are drawn non-indexed.
53827847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     * @param   indexCount      if indices is non-null then this is the
53927847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     *                          number of indices.
54027847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     */
54127847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    void drawVertices(const GrPaint& paint,
54227847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com                      GrPrimitiveType primitiveType,
54327847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com                      int vertexCount,
544972f9cd7a063d0544f8c919fd12b9a3adbd12b24commit-bot@chromium.org                      const SkPoint positions[],
545972f9cd7a063d0544f8c919fd12b9a3adbd12b24commit-bot@chromium.org                      const SkPoint texs[],
54627847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com                      const GrColor colors[],
54727847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com                      const uint16_t indices[],
54827847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com                      int indexCount);
54927847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
55093c9660cd158c5d0cab0ba4223e4257f699d5bb8bsalomon@google.com    /**
55193c9660cd158c5d0cab0ba4223e4257f699d5bb8bsalomon@google.com     * Draws an oval.
55293c9660cd158c5d0cab0ba4223e4257f699d5bb8bsalomon@google.com     *
55393c9660cd158c5d0cab0ba4223e4257f699d5bb8bsalomon@google.com     * @param paint         describes how to color pixels.
55446d3d39e65e0b3ea2ad7c91c176ccafb4df0fa24jvanverth@google.com     * @param oval          the bounding rect of the oval.
555d58a0ba9cff9fcefe5047e88ccb4a6e76c591c40egdaniel     * @param strokeInfo    the stroke information (width, join, cap) and
556d58a0ba9cff9fcefe5047e88ccb4a6e76c591c40egdaniel     *                      the dash information (intervals, count, phase).
55793c9660cd158c5d0cab0ba4223e4257f699d5bb8bsalomon@google.com     */
55893c9660cd158c5d0cab0ba4223e4257f699d5bb8bsalomon@google.com    void drawOval(const GrPaint& paint,
559fd03d4a829efe2d77a712fd991927c55f59a2ffecommit-bot@chromium.org                  const SkRect& oval,
560d58a0ba9cff9fcefe5047e88ccb4a6e76c591c40egdaniel                  const GrStrokeInfo& strokeInfo);
56193c9660cd158c5d0cab0ba4223e4257f699d5bb8bsalomon@google.com
56227847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    ///////////////////////////////////////////////////////////////////////////
56327847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    // Misc.
56427847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
56527847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    /**
566a7f84e150cb9e821267a8ca9e59c1ae9a3cfa300bsalomon@google.com     * Flags that affect flush() behavior.
567a7f84e150cb9e821267a8ca9e59c1ae9a3cfa300bsalomon@google.com     */
568a7f84e150cb9e821267a8ca9e59c1ae9a3cfa300bsalomon@google.com    enum FlushBits {
569a7f84e150cb9e821267a8ca9e59c1ae9a3cfa300bsalomon@google.com        /**
570a7f84e150cb9e821267a8ca9e59c1ae9a3cfa300bsalomon@google.com         * A client may reach a point where it has partially rendered a frame
571a7f84e150cb9e821267a8ca9e59c1ae9a3cfa300bsalomon@google.com         * through a GrContext that it knows the user will never see. This flag
572a7f84e150cb9e821267a8ca9e59c1ae9a3cfa300bsalomon@google.com         * causes the flush to skip submission of deferred content to the 3D API
573a7f84e150cb9e821267a8ca9e59c1ae9a3cfa300bsalomon@google.com         * during the flush.
574a7f84e150cb9e821267a8ca9e59c1ae9a3cfa300bsalomon@google.com         */
575a7f84e150cb9e821267a8ca9e59c1ae9a3cfa300bsalomon@google.com        kDiscard_FlushBit                    = 0x2,
576a7f84e150cb9e821267a8ca9e59c1ae9a3cfa300bsalomon@google.com    };
577a7f84e150cb9e821267a8ca9e59c1ae9a3cfa300bsalomon@google.com
578a7f84e150cb9e821267a8ca9e59c1ae9a3cfa300bsalomon@google.com    /**
57927847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     * Call to ensure all drawing to the context has been issued to the
58027847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     * underlying 3D API.
581a7f84e150cb9e821267a8ca9e59c1ae9a3cfa300bsalomon@google.com     * @param flagsBitfield     flags that control the flushing behavior. See
582a7f84e150cb9e821267a8ca9e59c1ae9a3cfa300bsalomon@google.com     *                          FlushBits.
58327847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     */
5848fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com    void flush(int flagsBitfield = 0);
5856f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com
5860342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com   /**
5870342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com    * These flags can be used with the read/write pixels functions below.
5880342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com    */
5890342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com    enum PixelOpsFlags {
5900342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com        /** The GrContext will not be flushed. This means that the read or write may occur before
5910342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com            previous draws have executed. */
5920342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com        kDontFlush_PixelOpsFlag = 0x1,
5930342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com        /** The src for write or dst read is unpremultiplied. This is only respected if both the
5940342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com            config src and dst configs are an RGBA/BGRA 8888 format. */
5950342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com        kUnpremul_PixelOpsFlag  = 0x2,
5960342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com    };
5970342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com
598669fdc4ed8ed461a141cb97d0afdd9ef72a82be1bsalomon@google.com    /**
599669fdc4ed8ed461a141cb97d0afdd9ef72a82be1bsalomon@google.com     * Reads a rectangle of pixels from a render target.
6000342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com     * @param target        the render target to read from. NULL means the current render target.
601669fdc4ed8ed461a141cb97d0afdd9ef72a82be1bsalomon@google.com     * @param left          left edge of the rectangle to read (inclusive)
602669fdc4ed8ed461a141cb97d0afdd9ef72a82be1bsalomon@google.com     * @param top           top edge of the rectangle to read (inclusive)
603669fdc4ed8ed461a141cb97d0afdd9ef72a82be1bsalomon@google.com     * @param width         width of rectangle to read in pixels.
604669fdc4ed8ed461a141cb97d0afdd9ef72a82be1bsalomon@google.com     * @param height        height of rectangle to read in pixels.
605669fdc4ed8ed461a141cb97d0afdd9ef72a82be1bsalomon@google.com     * @param config        the pixel config of the destination buffer
606669fdc4ed8ed461a141cb97d0afdd9ef72a82be1bsalomon@google.com     * @param buffer        memory to read the rectangle into.
6070342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com     * @param rowBytes      number of bytes bewtween consecutive rows. Zero means rows are tightly
6080342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com     *                      packed.
6090342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com     * @param pixelOpsFlags see PixelOpsFlags enum above.
610669fdc4ed8ed461a141cb97d0afdd9ef72a82be1bsalomon@google.com     *
6110342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com     * @return true if the read succeeded, false if not. The read can fail because of an unsupported
6120342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com     *         pixel config or because no render target is currently set and NULL was passed for
6130342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com     *         target.
614669fdc4ed8ed461a141cb97d0afdd9ef72a82be1bsalomon@google.com     */
615669fdc4ed8ed461a141cb97d0afdd9ef72a82be1bsalomon@google.com    bool readRenderTargetPixels(GrRenderTarget* target,
616669fdc4ed8ed461a141cb97d0afdd9ef72a82be1bsalomon@google.com                                int left, int top, int width, int height,
6170342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                                GrPixelConfig config, void* buffer,
6180342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                                size_t rowBytes = 0,
6190342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                                uint32_t pixelOpsFlags = 0);
6206f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com
6216f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com    /**
6221e269b5a08610da13c3aee23809bb45b17e7b663bsalomon@google.com     * Copy the src pixels [buffer, row bytes, pixel config] into a render target at the specified
6230342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com     * rectangle.
6240342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com     * @param target        the render target to write into. NULL means the current render target.
6256f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com     * @param left          left edge of the rectangle to write (inclusive)
6266f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com     * @param top           top edge of the rectangle to write (inclusive)
6276f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com     * @param width         width of rectangle to write in pixels.
6286f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com     * @param height        height of rectangle to write in pixels.
6296f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com     * @param config        the pixel config of the source buffer
6306f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com     * @param buffer        memory to read the rectangle from.
6313cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com     * @param rowBytes      number of bytes between consecutive rows. Zero means rows are tightly
6320342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com     *                      packed.
6330342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com     * @param pixelOpsFlags see PixelOpsFlags enum above.
6349c68058b679aee81e6e0158e7fcbfb5d8479c91absalomon@google.com     *
6359c68058b679aee81e6e0158e7fcbfb5d8479c91absalomon@google.com     * @return true if the write succeeded, false if not. The write can fail because of an
6369c68058b679aee81e6e0158e7fcbfb5d8479c91absalomon@google.com     *         unsupported combination of target and pixel configs.
6376f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com     */
6389c68058b679aee81e6e0158e7fcbfb5d8479c91absalomon@google.com    bool writeRenderTargetPixels(GrRenderTarget* target,
6396f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com                                 int left, int top, int width, int height,
6406f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com                                 GrPixelConfig config, const void* buffer,
6410342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                                 size_t rowBytes = 0,
6420342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                                 uint32_t pixelOpsFlags = 0);
643669fdc4ed8ed461a141cb97d0afdd9ef72a82be1bsalomon@google.com
644669fdc4ed8ed461a141cb97d0afdd9ef72a82be1bsalomon@google.com    /**
645669fdc4ed8ed461a141cb97d0afdd9ef72a82be1bsalomon@google.com     * Reads a rectangle of pixels from a texture.
6466f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com     * @param texture       the texture to read from.
647669fdc4ed8ed461a141cb97d0afdd9ef72a82be1bsalomon@google.com     * @param left          left edge of the rectangle to read (inclusive)
648669fdc4ed8ed461a141cb97d0afdd9ef72a82be1bsalomon@google.com     * @param top           top edge of the rectangle to read (inclusive)
649669fdc4ed8ed461a141cb97d0afdd9ef72a82be1bsalomon@google.com     * @param width         width of rectangle to read in pixels.
650669fdc4ed8ed461a141cb97d0afdd9ef72a82be1bsalomon@google.com     * @param height        height of rectangle to read in pixels.
651669fdc4ed8ed461a141cb97d0afdd9ef72a82be1bsalomon@google.com     * @param config        the pixel config of the destination buffer
652669fdc4ed8ed461a141cb97d0afdd9ef72a82be1bsalomon@google.com     * @param buffer        memory to read the rectangle into.
6533cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com     * @param rowBytes      number of bytes between consecutive rows. Zero means rows are tightly
6540342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com     *                      packed.
6550342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com     * @param pixelOpsFlags see PixelOpsFlags enum above.
656669fdc4ed8ed461a141cb97d0afdd9ef72a82be1bsalomon@google.com     *
6570342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com     * @return true if the read succeeded, false if not. The read can fail because of an unsupported
6580342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com     *         pixel config.
65927847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     */
6606f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com    bool readTexturePixels(GrTexture* texture,
661669fdc4ed8ed461a141cb97d0afdd9ef72a82be1bsalomon@google.com                           int left, int top, int width, int height,
6626f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com                           GrPixelConfig config, void* buffer,
6630342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                           size_t rowBytes = 0,
6640342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                           uint32_t pixelOpsFlags = 0);
66527847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
66627847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    /**
6676f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com     * Writes a rectangle of pixels to a texture.
6686f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com     * @param texture       the render target to read from.
6696f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com     * @param left          left edge of the rectangle to write (inclusive)
6706f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com     * @param top           top edge of the rectangle to write (inclusive)
6716f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com     * @param width         width of rectangle to write in pixels.
6726f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com     * @param height        height of rectangle to write in pixels.
6736f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com     * @param config        the pixel config of the source buffer
6746f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com     * @param buffer        memory to read pixels from
6753cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com     * @param rowBytes      number of bytes between consecutive rows. Zero
6766f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com     *                      means rows are tightly packed.
6770342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com     * @param pixelOpsFlags see PixelOpsFlags enum above.
6789c68058b679aee81e6e0158e7fcbfb5d8479c91absalomon@google.com     * @return true if the write succeeded, false if not. The write can fail because of an
6799c68058b679aee81e6e0158e7fcbfb5d8479c91absalomon@google.com     *         unsupported combination of texture and pixel configs.
68027847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     */
6819c68058b679aee81e6e0158e7fcbfb5d8479c91absalomon@google.com    bool writeTexturePixels(GrTexture* texture,
6826f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com                            int left, int top, int width, int height,
6836f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com                            GrPixelConfig config, const void* buffer,
6840342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                            size_t rowBytes,
6850342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                            uint32_t pixelOpsFlags = 0);
6860342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com
687027de5f922e8ad1acc5402829f716b8c513fdd8dsenorblanco@chromium.org    /**
688a2a31928470dfb642880f6ab2e4d34b1c7f5d476scroggo@google.com     * Copies a rectangle of texels from src to dst. The size of dst is the size of the rectangle
689a2a31928470dfb642880f6ab2e4d34b1c7f5d476scroggo@google.com     * copied and topLeft is the position of the rect in src. The rectangle is clipped to src's
690a2a31928470dfb642880f6ab2e4d34b1c7f5d476scroggo@google.com     * bounds.
691ef843cdcd1be858fe55cc99ff134ffbd080c9a51senorblanco@chromium.org     * @param src           the texture to copy from.
692ef843cdcd1be858fe55cc99ff134ffbd080c9a51senorblanco@chromium.org     * @param dst           the render target to copy to.
693a2a31928470dfb642880f6ab2e4d34b1c7f5d476scroggo@google.com     * @param topLeft       the point in src that will be copied to the top-left of dst. If NULL,
694a2a31928470dfb642880f6ab2e4d34b1c7f5d476scroggo@google.com     *                      (0, 0) will be used.
695ef843cdcd1be858fe55cc99ff134ffbd080c9a51senorblanco@chromium.org     */
696a2a31928470dfb642880f6ab2e4d34b1c7f5d476scroggo@google.com    void copyTexture(GrTexture* src, GrRenderTarget* dst, const SkIPoint* topLeft = NULL);
69775f9f25d8bf2adc0494f9afbbd5965809ee13acabsalomon@google.com
69875f9f25d8bf2adc0494f9afbbd5965809ee13acabsalomon@google.com    /**
69975f9f25d8bf2adc0494f9afbbd5965809ee13acabsalomon@google.com     * Resolves a render target that has MSAA. The intermediate MSAA buffer is
7001e269b5a08610da13c3aee23809bb45b17e7b663bsalomon@google.com     * down-sampled to the associated GrTexture (accessible via
70175f9f25d8bf2adc0494f9afbbd5965809ee13acabsalomon@google.com     * GrRenderTarget::asTexture()). Any pending draws to the render target will
70275f9f25d8bf2adc0494f9afbbd5965809ee13acabsalomon@google.com     * be executed before the resolve.
70375f9f25d8bf2adc0494f9afbbd5965809ee13acabsalomon@google.com     *
70475f9f25d8bf2adc0494f9afbbd5965809ee13acabsalomon@google.com     * This is only necessary when a client wants to access the object directly
70516e3ddea6a80972aced04b21b1d66377fa95e7c7bsalomon@google.com     * using the backend API directly. GrContext will detect when it must
70675f9f25d8bf2adc0494f9afbbd5965809ee13acabsalomon@google.com     * perform a resolve to a GrTexture used as the source of a draw or before
70775f9f25d8bf2adc0494f9afbbd5965809ee13acabsalomon@google.com     * reading pixels back from a GrTexture or GrRenderTarget.
70875f9f25d8bf2adc0494f9afbbd5965809ee13acabsalomon@google.com     */
70928361fad1054d59ed4e6a320c7a8b8782a1487c7commit-bot@chromium.org    void resolveRenderTarget(GrRenderTarget*);
71028361fad1054d59ed4e6a320c7a8b8782a1487c7commit-bot@chromium.org
71128361fad1054d59ed4e6a320c7a8b8782a1487c7commit-bot@chromium.org    /**
71228361fad1054d59ed4e6a320c7a8b8782a1487c7commit-bot@chromium.org     * Provides a perfomance hint that the render target's contents are allowed
71328361fad1054d59ed4e6a320c7a8b8782a1487c7commit-bot@chromium.org     * to become undefined.
71428361fad1054d59ed4e6a320c7a8b8782a1487c7commit-bot@chromium.org     */
71528361fad1054d59ed4e6a320c7a8b8782a1487c7commit-bot@chromium.org    void discardRenderTarget(GrRenderTarget*);
71675f9f25d8bf2adc0494f9afbbd5965809ee13acabsalomon@google.com
71703e3e89641c93df603dfb705d518848dfe81427ecommit-bot@chromium.org#ifdef SK_DEVELOPER
71803e3e89641c93df603dfb705d518848dfe81427ecommit-bot@chromium.org    void dumpFontCache() const;
71903e3e89641c93df603dfb705d518848dfe81427ecommit-bot@chromium.org#endif
72003e3e89641c93df603dfb705d518848dfe81427ecommit-bot@chromium.org
72127847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    ///////////////////////////////////////////////////////////////////////////
72227847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    // Helpers
72327847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
724a0b40280a49a8a43af7929ead3b3489951c58501commit-bot@chromium.org    class AutoRenderTarget : public ::SkNoncopyable {
72527847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    public:
72627847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com        AutoRenderTarget(GrContext* context, GrRenderTarget* target) {
72727847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com            fPrevTarget = context->getRenderTarget();
728a4de8c257ea0be8ff7081f645249b6afe5c48e7ecommit-bot@chromium.org            SkSafeRef(fPrevTarget);
729a04e8e842450e606dd938ddae17857849bd504d4bsalomon@google.com            context->setRenderTarget(target);
730a04e8e842450e606dd938ddae17857849bd504d4bsalomon@google.com            fContext = context;
731a04e8e842450e606dd938ddae17857849bd504d4bsalomon@google.com        }
732a04e8e842450e606dd938ddae17857849bd504d4bsalomon@google.com        AutoRenderTarget(GrContext* context) {
733a04e8e842450e606dd938ddae17857849bd504d4bsalomon@google.com            fPrevTarget = context->getRenderTarget();
734a4de8c257ea0be8ff7081f645249b6afe5c48e7ecommit-bot@chromium.org            SkSafeRef(fPrevTarget);
735a04e8e842450e606dd938ddae17857849bd504d4bsalomon@google.com            fContext = context;
73627847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com        }
73727847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com        ~AutoRenderTarget() {
738ccb39504096db647dafdb254cae59ae172969b8erobertphillips@google.com            if (NULL != fContext) {
73927847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com                fContext->setRenderTarget(fPrevTarget);
74027847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com            }
741a4de8c257ea0be8ff7081f645249b6afe5c48e7ecommit-bot@chromium.org            SkSafeUnref(fPrevTarget);
74227847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com        }
74327847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    private:
74427847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com        GrContext*      fContext;
74527847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com        GrRenderTarget* fPrevTarget;
74627847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    };
74727847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
748fea85ac3e31842f80493e2df8a93a28f21cd815frobertphillips@google.com    /**
7493cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com     * Save/restore the view-matrix in the context. It can optionally adjust a paint to account
7503cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com     * for a coordinate system change. Here is an example of how the paint param can be used:
7513cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com     *
7526f261bed0252e3f3caa595798364e0bf12a2573absalomon@google.com     * A GrPaint is setup with GrEffects. The stages will have access to the pre-matrix source
7533cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com     * geometry positions when the draw is executed. Later on a decision is made to transform the
7546f261bed0252e3f3caa595798364e0bf12a2573absalomon@google.com     * geometry to device space on the CPU. The effects now need to know that the space in which
7556f261bed0252e3f3caa595798364e0bf12a2573absalomon@google.com     * the geometry will be specified has changed.
7563cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com     *
7573cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com     * Note that when restore is called (or in the destructor) the context's matrix will be
7583cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com     * restored. However, the paint will not be restored. The caller must make a copy of the
7593cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com     * paint if necessary. Hint: use SkTCopyOnFirstWrite if the AutoMatrix is conditionally
7603cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com     * initialized.
761fea85ac3e31842f80493e2df8a93a28f21cd815frobertphillips@google.com     */
762a0b40280a49a8a43af7929ead3b3489951c58501commit-bot@chromium.org    class AutoMatrix : public ::SkNoncopyable {
763fea85ac3e31842f80493e2df8a93a28f21cd815frobertphillips@google.com    public:
764fea85ac3e31842f80493e2df8a93a28f21cd815frobertphillips@google.com        AutoMatrix() : fContext(NULL) {}
765a04e8e842450e606dd938ddae17857849bd504d4bsalomon@google.com
7663cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com        ~AutoMatrix() { this->restore(); }
7673cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com
7683cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com        /**
7693cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com         * Initializes by pre-concat'ing the context's current matrix with the preConcat param.
7703cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com         */
771b9086a026844e4cfd08b219e49ce3f12294cba98bsalomon@google.com        void setPreConcat(GrContext* context, const SkMatrix& preConcat, GrPaint* paint = NULL) {
772f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org            SkASSERT(NULL != context);
7733cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com
7743cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com            this->restore();
775f57c01bdcfdf1c923b9a473974bfe6f8c66eca3eskia.committer@gmail.com
7763cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com            fContext = context;
7773cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com            fMatrix = context->getMatrix();
7783cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com            this->preConcat(preConcat, paint);
779fea85ac3e31842f80493e2df8a93a28f21cd815frobertphillips@google.com        }
780a04e8e842450e606dd938ddae17857849bd504d4bsalomon@google.com
7813cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com        /**
7823cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com         * Sets the context's matrix to identity. Returns false if the inverse matrix is required to
7833cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com         * update a paint but the matrix cannot be inverted.
7843cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com         */
7853cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com        bool setIdentity(GrContext* context, GrPaint* paint = NULL) {
786f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org            SkASSERT(NULL != context);
7873cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com
7883cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com            this->restore();
789f57c01bdcfdf1c923b9a473974bfe6f8c66eca3eskia.committer@gmail.com
7903cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com            if (NULL != paint) {
791c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com                if (!paint->localCoordChangeInverse(context->getMatrix())) {
7923cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com                    return false;
7933cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com                }
7943cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com            }
7953cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com            fMatrix = context->getMatrix();
7963cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com            fContext = context;
7973cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com            context->setIdentityMatrix();
7983cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com            return true;
799fea85ac3e31842f80493e2df8a93a28f21cd815frobertphillips@google.com        }
800a04e8e842450e606dd938ddae17857849bd504d4bsalomon@google.com
8013cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com        /**
8023cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com         * Replaces the context's matrix with a new matrix. Returns false if the inverse matrix is
8033cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com         * required to update a paint but the matrix cannot be inverted.
8043cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com         */
805b9086a026844e4cfd08b219e49ce3f12294cba98bsalomon@google.com        bool set(GrContext* context, const SkMatrix& newMatrix, GrPaint* paint = NULL) {
8063cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com            if (NULL != paint) {
8073cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com                if (!this->setIdentity(context, paint)) {
8083cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com                    return false;
8093cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com                }
8103cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com                this->preConcat(newMatrix, paint);
8113cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com            } else {
8123cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com                this->restore();
8133cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com                fContext = context;
8143cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com                fMatrix = context->getMatrix();
8153cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com                context->setMatrix(newMatrix);
816fea85ac3e31842f80493e2df8a93a28f21cd815frobertphillips@google.com            }
8173cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com            return true;
818fea85ac3e31842f80493e2df8a93a28f21cd815frobertphillips@google.com        }
819a04e8e842450e606dd938ddae17857849bd504d4bsalomon@google.com
8203cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com        /**
8213cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com         * If this has been initialized then the context's matrix will be further updated by
8223cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com         * pre-concat'ing the preConcat param. The matrix that will be restored remains unchanged.
8233cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com         * The paint is assumed to be relative to the context's matrix at the time this call is
8243cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com         * made, not the matrix at the time AutoMatrix was first initialized. In other words, this
8253cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com         * performs an incremental update of the paint.
8263cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com         */
827b9086a026844e4cfd08b219e49ce3f12294cba98bsalomon@google.com        void preConcat(const SkMatrix& preConcat, GrPaint* paint = NULL) {
8283cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com            if (NULL != paint) {
829c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com                paint->localCoordChange(preConcat);
830fea85ac3e31842f80493e2df8a93a28f21cd815frobertphillips@google.com            }
8313cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com            fContext->concatMatrix(preConcat);
832fea85ac3e31842f80493e2df8a93a28f21cd815frobertphillips@google.com        }
833a04e8e842450e606dd938ddae17857849bd504d4bsalomon@google.com
8343cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com        /**
8353cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com         * Returns false if never initialized or the inverse matrix was required to update a paint
8363cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com         * but the matrix could not be inverted.
8373cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com         */
8383cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com        bool succeeded() const { return NULL != fContext; }
8393cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com
8403cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com        /**
8413cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com         * If this has been initialized then the context's original matrix is restored.
8423cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com         */
8433cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com        void restore() {
844fea85ac3e31842f80493e2df8a93a28f21cd815frobertphillips@google.com            if (NULL != fContext) {
845fea85ac3e31842f80493e2df8a93a28f21cd815frobertphillips@google.com                fContext->setMatrix(fMatrix);
8463cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com                fContext = NULL;
847fea85ac3e31842f80493e2df8a93a28f21cd815frobertphillips@google.com            }
848fea85ac3e31842f80493e2df8a93a28f21cd815frobertphillips@google.com        }
849fea85ac3e31842f80493e2df8a93a28f21cd815frobertphillips@google.com
850fea85ac3e31842f80493e2df8a93a28f21cd815frobertphillips@google.com    private:
851fea85ac3e31842f80493e2df8a93a28f21cd815frobertphillips@google.com        GrContext*  fContext;
852b9086a026844e4cfd08b219e49ce3f12294cba98bsalomon@google.com        SkMatrix    fMatrix;
853fea85ac3e31842f80493e2df8a93a28f21cd815frobertphillips@google.com    };
85427847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
855a0b40280a49a8a43af7929ead3b3489951c58501commit-bot@chromium.org    class AutoClip : public ::SkNoncopyable {
85656c79b10377e358b8092d9c6ab3e1aacf2cd60e5robertphillips@google.com    public:
857a04e8e842450e606dd938ddae17857849bd504d4bsalomon@google.com        // This enum exists to require a caller of the constructor to acknowledge that the clip will
858a04e8e842450e606dd938ddae17857849bd504d4bsalomon@google.com        // initially be wide open. It also could be extended if there are other desirable initial
859a04e8e842450e606dd938ddae17857849bd504d4bsalomon@google.com        // clip states.
860a04e8e842450e606dd938ddae17857849bd504d4bsalomon@google.com        enum InitialClip {
861a04e8e842450e606dd938ddae17857849bd504d4bsalomon@google.com            kWideOpen_InitialClip,
862a04e8e842450e606dd938ddae17857849bd504d4bsalomon@google.com        };
863a04e8e842450e606dd938ddae17857849bd504d4bsalomon@google.com
864dc3a4e5fa8c42bd0313af547bc15db2e6554a217skia.committer@gmail.com        AutoClip(GrContext* context, InitialClip initialState)
865ccb39504096db647dafdb254cae59ae172969b8erobertphillips@google.com        : fContext(context) {
866f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org            SkASSERT(kWideOpen_InitialClip == initialState);
867a04e8e842450e606dd938ddae17857849bd504d4bsalomon@google.com            fNewClipData.fClipStack = &fNewClipStack;
868ccb39504096db647dafdb254cae59ae172969b8erobertphillips@google.com
869ccb39504096db647dafdb254cae59ae172969b8erobertphillips@google.com            fOldClip = context->getClip();
870a04e8e842450e606dd938ddae17857849bd504d4bsalomon@google.com            context->setClip(&fNewClipData);
871a04e8e842450e606dd938ddae17857849bd504d4bsalomon@google.com        }
872a04e8e842450e606dd938ddae17857849bd504d4bsalomon@google.com
873fd03d4a829efe2d77a712fd991927c55f59a2ffecommit-bot@chromium.org        AutoClip(GrContext* context, const SkRect& newClipRect)
87456c79b10377e358b8092d9c6ab3e1aacf2cd60e5robertphillips@google.com        : fContext(context)
875beb1af78d016d2700c350487a383c6bcfa7e2e20robertphillips@google.com        , fNewClipStack(newClipRect) {
876beb1af78d016d2700c350487a383c6bcfa7e2e20robertphillips@google.com            fNewClipData.fClipStack = &fNewClipStack;
877beb1af78d016d2700c350487a383c6bcfa7e2e20robertphillips@google.com
87856c79b10377e358b8092d9c6ab3e1aacf2cd60e5robertphillips@google.com            fOldClip = fContext->getClip();
879beb1af78d016d2700c350487a383c6bcfa7e2e20robertphillips@google.com            fContext->setClip(&fNewClipData);
88056c79b10377e358b8092d9c6ab3e1aacf2cd60e5robertphillips@google.com        }
88156c79b10377e358b8092d9c6ab3e1aacf2cd60e5robertphillips@google.com
88256c79b10377e358b8092d9c6ab3e1aacf2cd60e5robertphillips@google.com        ~AutoClip() {
88356c79b10377e358b8092d9c6ab3e1aacf2cd60e5robertphillips@google.com            if (NULL != fContext) {
88456c79b10377e358b8092d9c6ab3e1aacf2cd60e5robertphillips@google.com                fContext->setClip(fOldClip);
88556c79b10377e358b8092d9c6ab3e1aacf2cd60e5robertphillips@google.com            }
88656c79b10377e358b8092d9c6ab3e1aacf2cd60e5robertphillips@google.com        }
88756c79b10377e358b8092d9c6ab3e1aacf2cd60e5robertphillips@google.com    private:
888beb1af78d016d2700c350487a383c6bcfa7e2e20robertphillips@google.com        GrContext*        fContext;
889beb1af78d016d2700c350487a383c6bcfa7e2e20robertphillips@google.com        const GrClipData* fOldClip;
890beb1af78d016d2700c350487a383c6bcfa7e2e20robertphillips@google.com
891641f8b19a6799b6d73ac17b9c2d2f8a5e6f5ad4drobertphillips@google.com        SkClipStack       fNewClipStack;
892beb1af78d016d2700c350487a383c6bcfa7e2e20robertphillips@google.com        GrClipData        fNewClipData;
89356c79b10377e358b8092d9c6ab3e1aacf2cd60e5robertphillips@google.com    };
89456c79b10377e358b8092d9c6ab3e1aacf2cd60e5robertphillips@google.com
895a04e8e842450e606dd938ddae17857849bd504d4bsalomon@google.com    class AutoWideOpenIdentityDraw {
896a04e8e842450e606dd938ddae17857849bd504d4bsalomon@google.com    public:
897a04e8e842450e606dd938ddae17857849bd504d4bsalomon@google.com        AutoWideOpenIdentityDraw(GrContext* ctx, GrRenderTarget* rt)
898a04e8e842450e606dd938ddae17857849bd504d4bsalomon@google.com            : fAutoClip(ctx, AutoClip::kWideOpen_InitialClip)
8993cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com            , fAutoRT(ctx, rt) {
9003cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com            fAutoMatrix.setIdentity(ctx);
9013cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com            // should never fail with no paint param.
902f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org            SkASSERT(fAutoMatrix.succeeded());
903a04e8e842450e606dd938ddae17857849bd504d4bsalomon@google.com        }
9043cbaa2d4da8bc39a99bf3afaaab43cc6dc481723bsalomon@google.com
905a04e8e842450e606dd938ddae17857849bd504d4bsalomon@google.com    private:
906a04e8e842450e606dd938ddae17857849bd504d4bsalomon@google.com        AutoClip fAutoClip;
907a04e8e842450e606dd938ddae17857849bd504d4bsalomon@google.com        AutoRenderTarget fAutoRT;
908a04e8e842450e606dd938ddae17857849bd504d4bsalomon@google.com        AutoMatrix fAutoMatrix;
909a04e8e842450e606dd938ddae17857849bd504d4bsalomon@google.com    };
910a04e8e842450e606dd938ddae17857849bd504d4bsalomon@google.com
91127847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    ///////////////////////////////////////////////////////////////////////////
91227847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    // Functions intended for internal use only.
91327847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    GrGpu* getGpu() { return fGpu; }
9141f221a70214fa1ab87b8a32dd66facf485f318eebsalomon@google.com    const GrGpu* getGpu() const { return fGpu; }
91527847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    GrFontCache* getFontCache() { return fFontCache; }
916e930a075625d3d90c023c2570a6a4cae1a4b484frobertphillips@google.com    GrLayerCache* getLayerCache() { return fLayerCache.get(); }
91721c10c5ff50cb8f2fd995fc9396d435d3396258bbsalomon@google.com    GrDrawTarget* getTextTarget();
91827847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    const GrIndexBuffer* getQuadIndexBuffer() const;
91995c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org    GrAARectRenderer* getAARectRenderer() { return fAARectRenderer; }
9209923c2b29aafc7ebc81c929c68c6b32443c9f22bbsalomon@google.com
92178a1078f17f4f0ae63415298517262a64f706af6commit-bot@chromium.org    // Called by tests that draw directly to the context via GrDrawTarget
92278a1078f17f4f0ae63415298517262a64f706af6commit-bot@chromium.org    void getTestTarget(GrTestTarget*);
92378a1078f17f4f0ae63415298517262a64f706af6commit-bot@chromium.org
9242a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org    // Functions for managing gpu trace markers
9252a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org    bool isGpuTracingEnabled() const { return fGpuTracingEnabled; }
9262a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org    void enableGpuTracing() { fGpuTracingEnabled = true; }
9272a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org    void disableGpuTracing() { fGpuTracingEnabled = false; }
9282a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org
929bbcb38df4f7477aa0a38da00a0757586a0125954egdaniel    void addGpuTraceMarker(const GrGpuTraceMarker* marker);
930bbcb38df4f7477aa0a38da00a0757586a0125954egdaniel    void removeGpuTraceMarker(const GrGpuTraceMarker* marker);
931bbcb38df4f7477aa0a38da00a0757586a0125954egdaniel
932558a75bcb323c03dd7482555c7214062a363276fbsalomon@google.com    /**
9331e269b5a08610da13c3aee23809bb45b17e7b663bsalomon@google.com     * Stencil buffers add themselves to the cache using addStencilBuffer. findStencilBuffer is
9341e269b5a08610da13c3aee23809bb45b17e7b663bsalomon@google.com     * called to check the cache for a SB that matches an RT's criteria.
935558a75bcb323c03dd7482555c7214062a363276fbsalomon@google.com     */
9369fbcad0f00d7098574cf3394a812c9d845c9cc5brobertphillips@google.com    void addStencilBuffer(GrStencilBuffer* sb);
9379fbcad0f00d7098574cf3394a812c9d845c9cc5brobertphillips@google.com    GrStencilBuffer* findStencilBuffer(int width, int height, int sampleCnt);
93827847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
93945a15f551b5b3c6c747d8eaf6466b7d3b76a8faebsalomon@google.com    GrPathRenderer* getPathRenderer(
94045a15f551b5b3c6c747d8eaf6466b7d3b76a8faebsalomon@google.com                    const SkPath& path,
9415f74cf8c49701f514b69dc6f1a8b5c0ffd78af0asugoi@google.com                    const SkStrokeRec& stroke,
94245a15f551b5b3c6c747d8eaf6466b7d3b76a8faebsalomon@google.com                    const GrDrawTarget* target,
94345a15f551b5b3c6c747d8eaf6466b7d3b76a8faebsalomon@google.com                    bool allowSW,
944e79f320ed6c5ec9f6164ba84be1ff586532e6517robertphillips@google.com                    GrPathRendererChain::DrawType drawType = GrPathRendererChain::kColor_DrawType,
94545a15f551b5b3c6c747d8eaf6466b7d3b76a8faebsalomon@google.com                    GrPathRendererChain::StencilSupport* stencilSupport = NULL);
9462c75681e36b33fcafc5665d7012bbd4fc6647d83robertphillips@google.com
9475955202c805c7ef1448103cbf666972ea9d1ded1robertphillips@google.com#if GR_CACHE_STATS
9485f9f2f574fe9b195f5f3e40edeb2e28b673511fdrobertphillips@google.com    void printCacheStats() const;
9495f9f2f574fe9b195f5f3e40edeb2e28b673511fdrobertphillips@google.com#endif
9505f9f2f574fe9b195f5f3e40edeb2e28b673511fdrobertphillips@google.com
95127847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.comprivate:
9521d4edd38f6b62d1cc54cb7bc6f3c6ce10ccb1471bsalomon@google.com    // Used to indicate whether a draw should be performed immediately or queued in fDrawBuffer.
9531d4edd38f6b62d1cc54cb7bc6f3c6ce10ccb1471bsalomon@google.com    enum BufferedDraw {
9541d4edd38f6b62d1cc54cb7bc6f3c6ce10ccb1471bsalomon@google.com        kYes_BufferedDraw,
9551d4edd38f6b62d1cc54cb7bc6f3c6ce10ccb1471bsalomon@google.com        kNo_BufferedDraw,
95627847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    };
9571d4edd38f6b62d1cc54cb7bc6f3c6ce10ccb1471bsalomon@google.com    BufferedDraw fLastDrawWasBuffered;
95827847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
959bb6a3178c3e79c8549b332e4ce84c64b59964f1ecommit-bot@chromium.org    GrGpu*                          fGpu;
960bb6a3178c3e79c8549b332e4ce84c64b59964f1ecommit-bot@chromium.org    SkMatrix                        fViewMatrix;
961bb6a3178c3e79c8549b332e4ce84c64b59964f1ecommit-bot@chromium.org    SkAutoTUnref<GrRenderTarget>    fRenderTarget;
962bb6a3178c3e79c8549b332e4ce84c64b59964f1ecommit-bot@chromium.org    const GrClipData*               fClip;  // TODO: make this ref counted
963bb6a3178c3e79c8549b332e4ce84c64b59964f1ecommit-bot@chromium.org    GrDrawState*                    fDrawState;
96410e04bf1c5025389d999f22467405782e9f9ffccbsalomon@google.com
96595c2003740c4cd01fd1b02ed93b9de7227b1d0f5commit-bot@chromium.org    GrResourceCache*                fResourceCache;
966bb6a3178c3e79c8549b332e4ce84c64b59964f1ecommit-bot@chromium.org    GrFontCache*                    fFontCache;
967e930a075625d3d90c023c2570a6a4cae1a4b484frobertphillips@google.com    SkAutoTDelete<GrLayerCache>     fLayerCache;
968dfe75bcf98b0c04535efbf7fe36492a7fb53c90dbsalomon@google.com
969bb6a3178c3e79c8549b332e4ce84c64b59964f1ecommit-bot@chromium.org    GrPathRendererChain*            fPathRendererChain;
970bb6a3178c3e79c8549b332e4ce84c64b59964f1ecommit-bot@chromium.org    GrSoftwarePathRenderer*         fSoftwarePathRenderer;
97127847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
972bb6a3178c3e79c8549b332e4ce84c64b59964f1ecommit-bot@chromium.org    GrVertexBufferAllocPool*        fDrawBufferVBAllocPool;
973bb6a3178c3e79c8549b332e4ce84c64b59964f1ecommit-bot@chromium.org    GrIndexBufferAllocPool*         fDrawBufferIBAllocPool;
974bb6a3178c3e79c8549b332e4ce84c64b59964f1ecommit-bot@chromium.org    GrInOrderDrawBuffer*            fDrawBuffer;
97527847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
9765dbb149fda134941e5d168790127caf294d322e2commit-bot@chromium.org    // Set by OverbudgetCB() to request that GrContext flush before exiting a draw.
9775dbb149fda134941e5d168790127caf294d322e2commit-bot@chromium.org    bool                            fFlushToReduceCacheSize;
9785dbb149fda134941e5d168790127caf294d322e2commit-bot@chromium.org
979bb6a3178c3e79c8549b332e4ce84c64b59964f1ecommit-bot@chromium.org    GrAARectRenderer*               fAARectRenderer;
980bb6a3178c3e79c8549b332e4ce84c64b59964f1ecommit-bot@chromium.org    GrOvalRenderer*                 fOvalRenderer;
981dfe75bcf98b0c04535efbf7fe36492a7fb53c90dbsalomon@google.com
982bb6a3178c3e79c8549b332e4ce84c64b59964f1ecommit-bot@chromium.org    bool                            fDidTestPMConversions;
983bb6a3178c3e79c8549b332e4ce84c64b59964f1ecommit-bot@chromium.org    int                             fPMToUPMConversion;
984bb6a3178c3e79c8549b332e4ce84c64b59964f1ecommit-bot@chromium.org    int                             fUPMToPMConversion;
985a04e8e842450e606dd938ddae17857849bd504d4bsalomon@google.com
986cdb426d55a4bc4dae4f6d4f23e6994762950fdc3robertphillips@google.com    struct CleanUpData {
987cdb426d55a4bc4dae4f6d4f23e6994762950fdc3robertphillips@google.com        PFCleanUpFunc fFunc;
988cdb426d55a4bc4dae4f6d4f23e6994762950fdc3robertphillips@google.com        void*         fInfo;
989cdb426d55a4bc4dae4f6d4f23e6994762950fdc3robertphillips@google.com    };
990cdb426d55a4bc4dae4f6d4f23e6994762950fdc3robertphillips@google.com
99144a91dcf2542c2aa93f159ebbb5bf813ae7362c0robertphillips@google.com    SkTDArray<CleanUpData>          fCleanUpData;
99244a91dcf2542c2aa93f159ebbb5bf813ae7362c0robertphillips@google.com
993956b310f13c7412c035406c658ff16ca85eac656skia.committer@gmail.com    int                             fMaxTextureSizeOverride;
994cdb426d55a4bc4dae4f6d4f23e6994762950fdc3robertphillips@google.com
9952a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org    bool                            fGpuTracingEnabled;
9962a05de0c049a8648942a55016126a1f92e1c14d6commit-bot@chromium.org
9976e4e65066a7c0dbc9bfbfe4b8f5d49c3d8a79b59bsalomon@google.com    GrContext(); // init must be called after the constructor.
9986e4e65066a7c0dbc9bfbfe4b8f5d49c3d8a79b59bsalomon@google.com    bool init(GrBackend, GrBackendContext);
999205d46067ace24bd4b111cf44efc96caff6c4d30bsalomon@google.com
10008fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com    void setupDrawBuffer();
10018fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
1002eb6879f50a5564eeb981ec5616b55bf685eb76fcbsalomon@google.com    class AutoRestoreEffects;
10035dbb149fda134941e5d168790127caf294d322e2commit-bot@chromium.org    class AutoCheckFlush;
100407ea2db0260d8e6cd2bf605571b68b1c574b5a77bsalomon@google.com    /// Sets the paint and returns the target to draw into. The paint can be NULL in which case the
100507ea2db0260d8e6cd2bf605571b68b1c574b5a77bsalomon@google.com    /// draw state is left unmodified.
10065dbb149fda134941e5d168790127caf294d322e2commit-bot@chromium.org    GrDrawTarget* prepareToDraw(const GrPaint*, BufferedDraw, AutoRestoreEffects*, AutoCheckFlush*);
100727847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
100837d883d9d3c5945e9d121830bdcd1935160fd53bcommit-bot@chromium.org    void internalDrawPath(GrDrawTarget* target, bool useAA, const SkPath& path,
1009d58a0ba9cff9fcefe5047e88ccb4a6e76c591c40egdaniel                          const GrStrokeInfo& stroke);
101046d3d39e65e0b3ea2ad7c91c176ccafb4df0fa24jvanverth@google.com
10113319f33470abc50a6f3da3a565d917050f9b2f53robertphillips@google.com    GrTexture* createResizedTexture(const GrTextureDesc& desc,
10120797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com                                    const GrCacheID& cacheID,
1013f9bd04faffb936602f7f957413a8cd5b7fc44addcommit-bot@chromium.org                                    const void* srcData,
10143319f33470abc50a6f3da3a565d917050f9b2f53robertphillips@google.com                                    size_t rowBytes,
1015b86add1ad37776818e1f730359ec587c9fdbff5fhumper@google.com                                    bool filter);
10163319f33470abc50a6f3da3a565d917050f9b2f53robertphillips@google.com
101715c0fea699b25343fe6f49668a5632866e1a0306robertphillips@google.com    // Needed so GrTexture's returnToCache helper function can call
101815c0fea699b25343fe6f49668a5632866e1a0306robertphillips@google.com    // addExistingTextureToCache
101915c0fea699b25343fe6f49668a5632866e1a0306robertphillips@google.com    friend class GrTexture;
10205c8ee2539b9316b22416a991a1f560ef5cec7957commit-bot@chromium.org    friend class GrStencilAndCoverPathRenderer;
102115c0fea699b25343fe6f49668a5632866e1a0306robertphillips@google.com
102215c0fea699b25343fe6f49668a5632866e1a0306robertphillips@google.com    // Add an existing texture to the texture cache. This is intended solely
102315c0fea699b25343fe6f49668a5632866e1a0306robertphillips@google.com    // for use with textures released from an GrAutoScratchTexture.
102415c0fea699b25343fe6f49668a5632866e1a0306robertphillips@google.com    void addExistingTextureToCache(GrTexture* texture);
1025fa35e3ddcc9d130ce87c927218bdf27879c38711reed@google.com
1026cdcb2ce2744c7e5c47453328dbf292edee79ab37skia.committer@gmail.com    /**
1027adc6536fe5baff2216fb76ecda6cc81c61109d5cbsalomon@google.com     * These functions create premul <-> unpremul effects if it is possible to generate a pair
1028adc6536fe5baff2216fb76ecda6cc81c61109d5cbsalomon@google.com     * of effects that make a readToUPM->writeToPM->readToUPM cycle invariant. Otherwise, they
1029adc6536fe5baff2216fb76ecda6cc81c61109d5cbsalomon@google.com     * return NULL.
1030adc6536fe5baff2216fb76ecda6cc81c61109d5cbsalomon@google.com     */
1031adc6536fe5baff2216fb76ecda6cc81c61109d5cbsalomon@google.com    const GrEffectRef* createPMToUPMEffect(GrTexture* texture,
1032adc6536fe5baff2216fb76ecda6cc81c61109d5cbsalomon@google.com                                           bool swapRAndB,
1033adc6536fe5baff2216fb76ecda6cc81c61109d5cbsalomon@google.com                                           const SkMatrix& matrix);
1034adc6536fe5baff2216fb76ecda6cc81c61109d5cbsalomon@google.com    const GrEffectRef* createUPMToPMEffect(GrTexture* texture,
1035adc6536fe5baff2216fb76ecda6cc81c61109d5cbsalomon@google.com                                           bool swapRAndB,
1036adc6536fe5baff2216fb76ecda6cc81c61109d5cbsalomon@google.com                                           const SkMatrix& matrix);
1037a04e8e842450e606dd938ddae17857849bd504d4bsalomon@google.com
1038cae27fed5e45e8899e56ece22b27e8958ffc0162commit-bot@chromium.org    /**
1039cae27fed5e45e8899e56ece22b27e8958ffc0162commit-bot@chromium.org     *  This callback allows the resource cache to callback into the GrContext
1040cae27fed5e45e8899e56ece22b27e8958ffc0162commit-bot@chromium.org     *  when the cache is still overbudget after a purge.
1041cae27fed5e45e8899e56ece22b27e8958ffc0162commit-bot@chromium.org     */
1042cae27fed5e45e8899e56ece22b27e8958ffc0162commit-bot@chromium.org    static bool OverbudgetCB(void* data);
1043cae27fed5e45e8899e56ece22b27e8958ffc0162commit-bot@chromium.org
10445c8ee2539b9316b22416a991a1f560ef5cec7957commit-bot@chromium.org    /** Creates a new gpu path, based on the specified path and stroke and returns it.
10455c8ee2539b9316b22416a991a1f560ef5cec7957commit-bot@chromium.org     * The caller owns a ref on the returned path which must be balanced by a call to unref.
10465c8ee2539b9316b22416a991a1f560ef5cec7957commit-bot@chromium.org     *
10475c8ee2539b9316b22416a991a1f560ef5cec7957commit-bot@chromium.org     * @param skPath the path geometry.
10485c8ee2539b9316b22416a991a1f560ef5cec7957commit-bot@chromium.org     * @param stroke the path stroke.
10495c8ee2539b9316b22416a991a1f560ef5cec7957commit-bot@chromium.org     * @return a new path or NULL if the operation is not supported by the backend.
10505c8ee2539b9316b22416a991a1f560ef5cec7957commit-bot@chromium.org     */
10515c8ee2539b9316b22416a991a1f560ef5cec7957commit-bot@chromium.org    GrPath* createPath(const SkPath& skPath, const SkStrokeRec& stroke);
10525c8ee2539b9316b22416a991a1f560ef5cec7957commit-bot@chromium.org
1053a4de8c257ea0be8ff7081f645249b6afe5c48e7ecommit-bot@chromium.org    typedef SkRefCnt INHERITED;
105427847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com};
105527847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
105627847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com/**
10571e269b5a08610da13c3aee23809bb45b17e7b663bsalomon@google.com * Gets and locks a scratch texture from a descriptor using either exact or approximate criteria.
10581e269b5a08610da13c3aee23809bb45b17e7b663bsalomon@google.com * Unlocks texture in the destructor.
1059aadd9f8b193aa70e3dc30cbe5c4e1bc5c1d047basenorblanco@chromium.org */
1060a0b40280a49a8a43af7929ead3b3489951c58501commit-bot@chromium.orgclass GrAutoScratchTexture : public ::SkNoncopyable {
1061aadd9f8b193aa70e3dc30cbe5c4e1bc5c1d047basenorblanco@chromium.orgpublic:
106250398bf7f1953e640e5529616e710cf540799731bsalomon@google.com    GrAutoScratchTexture()
10631f47f4f7325971dd53991e2bb02da94fa7c6d962robertphillips@google.com        : fContext(NULL)
10641f47f4f7325971dd53991e2bb02da94fa7c6d962robertphillips@google.com        , fTexture(NULL) {
1065aadd9f8b193aa70e3dc30cbe5c4e1bc5c1d047basenorblanco@chromium.org    }
106650398bf7f1953e640e5529616e710cf540799731bsalomon@google.com
106750398bf7f1953e640e5529616e710cf540799731bsalomon@google.com    GrAutoScratchTexture(GrContext* context,
106850398bf7f1953e640e5529616e710cf540799731bsalomon@google.com                         const GrTextureDesc& desc,
10690797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com                         GrContext::ScratchTexMatch match = GrContext::kApprox_ScratchTexMatch)
10701f47f4f7325971dd53991e2bb02da94fa7c6d962robertphillips@google.com      : fContext(NULL)
10711f47f4f7325971dd53991e2bb02da94fa7c6d962robertphillips@google.com      , fTexture(NULL) {
107250398bf7f1953e640e5529616e710cf540799731bsalomon@google.com      this->set(context, desc, match);
107350398bf7f1953e640e5529616e710cf540799731bsalomon@google.com    }
1074fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
107550398bf7f1953e640e5529616e710cf540799731bsalomon@google.com    ~GrAutoScratchTexture() {
10769ec075366ed94802a142fc5e948d19a774f85b43robertphillips@google.com        this->reset();
10779ec075366ed94802a142fc5e948d19a774f85b43robertphillips@google.com    }
10789ec075366ed94802a142fc5e948d19a774f85b43robertphillips@google.com
10799ec075366ed94802a142fc5e948d19a774f85b43robertphillips@google.com    void reset() {
10801f47f4f7325971dd53991e2bb02da94fa7c6d962robertphillips@google.com        if (NULL != fContext && NULL != fTexture) {
10819fbcad0f00d7098574cf3394a812c9d845c9cc5brobertphillips@google.com            fContext->unlockScratchTexture(fTexture);
108295ed55adc6b8f0cee063c2cf2e14782773b0087fbsalomon@google.com            fTexture->unref();
10831f47f4f7325971dd53991e2bb02da94fa7c6d962robertphillips@google.com            fTexture = NULL;
1084aadd9f8b193aa70e3dc30cbe5c4e1bc5c1d047basenorblanco@chromium.org        }
1085aadd9f8b193aa70e3dc30cbe5c4e1bc5c1d047basenorblanco@chromium.org    }
108684223118f7073a28f42c11ad08595e07c0ceb933bsalomon@google.com
108715c0fea699b25343fe6f49668a5632866e1a0306robertphillips@google.com    /*
108815c0fea699b25343fe6f49668a5632866e1a0306robertphillips@google.com     * When detaching a texture we do not unlock it in the texture cache but
1089fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com     * we do set the returnToCache flag. In this way the texture remains
1090fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com     * "locked" in the texture cache until it is freed and recycled in
1091fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com     * GrTexture::internal_dispose. In reality, the texture has been removed
1092fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com     * from the cache (because this is in AutoScratchTexture) and by not
109395ed55adc6b8f0cee063c2cf2e14782773b0087fbsalomon@google.com     * calling unlockScratchTexture we simply don't re-add it. It will be
109495ed55adc6b8f0cee063c2cf2e14782773b0087fbsalomon@google.com     * reattached in GrTexture::internal_dispose.
109515c0fea699b25343fe6f49668a5632866e1a0306robertphillips@google.com     *
109615c0fea699b25343fe6f49668a5632866e1a0306robertphillips@google.com     * Note that the caller is assumed to accept and manage the ref to the
109715c0fea699b25343fe6f49668a5632866e1a0306robertphillips@google.com     * returned texture.
109815c0fea699b25343fe6f49668a5632866e1a0306robertphillips@google.com     */
109915c0fea699b25343fe6f49668a5632866e1a0306robertphillips@google.com    GrTexture* detach() {
11003f4ea26ff5794547c4c7ff40b265700fff551521commit-bot@chromium.org        if (NULL == fTexture) {
11013f4ea26ff5794547c4c7ff40b265700fff551521commit-bot@chromium.org            return NULL;
11023f4ea26ff5794547c4c7ff40b265700fff551521commit-bot@chromium.org        }
110395ed55adc6b8f0cee063c2cf2e14782773b0087fbsalomon@google.com        GrTexture* texture = fTexture;
11041f47f4f7325971dd53991e2bb02da94fa7c6d962robertphillips@google.com        fTexture = NULL;
110515c0fea699b25343fe6f49668a5632866e1a0306robertphillips@google.com
110695ed55adc6b8f0cee063c2cf2e14782773b0087fbsalomon@google.com        // This GrAutoScratchTexture has a ref from lockAndRefScratchTexture, which we give up now.
110795ed55adc6b8f0cee063c2cf2e14782773b0087fbsalomon@google.com        // The cache also has a ref which we are lending to the caller of detach(). When the caller
110895ed55adc6b8f0cee063c2cf2e14782773b0087fbsalomon@google.com        // lets go of the ref and the ref count goes to 0 internal_dispose will see this flag is
110995ed55adc6b8f0cee063c2cf2e14782773b0087fbsalomon@google.com        // set and re-ref the texture, thereby restoring the cache's ref.
1110f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org        SkASSERT(texture->getRefCnt() > 1);
1111e49157f0838c615b10da0ef2b297c742da662e34commit-bot@chromium.org        texture->impl()->setFlag((GrTextureFlags) GrTextureImpl::kReturnToCache_FlagBit);
111295ed55adc6b8f0cee063c2cf2e14782773b0087fbsalomon@google.com        texture->unref();
1113f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org        SkASSERT(NULL != texture->getCacheEntry());
111495ed55adc6b8f0cee063c2cf2e14782773b0087fbsalomon@google.com
111595ed55adc6b8f0cee063c2cf2e14782773b0087fbsalomon@google.com        return texture;
111615c0fea699b25343fe6f49668a5632866e1a0306robertphillips@google.com    }
111715c0fea699b25343fe6f49668a5632866e1a0306robertphillips@google.com
111850398bf7f1953e640e5529616e710cf540799731bsalomon@google.com    GrTexture* set(GrContext* context,
111950398bf7f1953e640e5529616e710cf540799731bsalomon@google.com                   const GrTextureDesc& desc,
11200797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com                   GrContext::ScratchTexMatch match = GrContext::kApprox_ScratchTexMatch) {
11219ec075366ed94802a142fc5e948d19a774f85b43robertphillips@google.com        this->reset();
11229ec075366ed94802a142fc5e948d19a774f85b43robertphillips@google.com
112350398bf7f1953e640e5529616e710cf540799731bsalomon@google.com        fContext = context;
112450398bf7f1953e640e5529616e710cf540799731bsalomon@google.com        if (NULL != fContext) {
112595ed55adc6b8f0cee063c2cf2e14782773b0087fbsalomon@google.com            fTexture = fContext->lockAndRefScratchTexture(desc, match);
11261f47f4f7325971dd53991e2bb02da94fa7c6d962robertphillips@google.com            if (NULL == fTexture) {
112750398bf7f1953e640e5529616e710cf540799731bsalomon@google.com                fContext = NULL;
112850398bf7f1953e640e5529616e710cf540799731bsalomon@google.com            }
11291f47f4f7325971dd53991e2bb02da94fa7c6d962robertphillips@google.com            return fTexture;
113050398bf7f1953e640e5529616e710cf540799731bsalomon@google.com        } else {
113150398bf7f1953e640e5529616e710cf540799731bsalomon@google.com            return NULL;
113250398bf7f1953e640e5529616e710cf540799731bsalomon@google.com        }
113350398bf7f1953e640e5529616e710cf540799731bsalomon@google.com    }
113450398bf7f1953e640e5529616e710cf540799731bsalomon@google.com
11351f47f4f7325971dd53991e2bb02da94fa7c6d962robertphillips@google.com    GrTexture* texture() { return fTexture; }
11361f47f4f7325971dd53991e2bb02da94fa7c6d962robertphillips@google.com
1137aadd9f8b193aa70e3dc30cbe5c4e1bc5c1d047basenorblanco@chromium.orgprivate:
113850398bf7f1953e640e5529616e710cf540799731bsalomon@google.com    GrContext*                    fContext;
11391f47f4f7325971dd53991e2bb02da94fa7c6d962robertphillips@google.com    GrTexture*                    fTexture;
1140aadd9f8b193aa70e3dc30cbe5c4e1bc5c1d047basenorblanco@chromium.org};
1141aadd9f8b193aa70e3dc30cbe5c4e1bc5c1d047basenorblanco@chromium.org
114227847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com#endif
1143