GrRenderTarget.h revision fd03d4a829efe2d77a712fd991927c55f59a2ffe
1aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com/*
2dd335aeb5d34a8344f98244d722fd205b8e05135reed@google.com * Copyright 2011 Google Inc.
3dd335aeb5d34a8344f98244d722fd205b8e05135reed@google.com *
4dd335aeb5d34a8344f98244d722fd205b8e05135reed@google.com * Use of this source code is governed by a BSD-style license that can be
5dd335aeb5d34a8344f98244d722fd205b8e05135reed@google.com * found in the LICENSE file.
6aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com */
7aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
8aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com#ifndef GrRenderTarget_DEFINED
9aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com#define GrRenderTarget_DEFINED
10aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
117d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com#include "GrSurface.h"
12fd03d4a829efe2d77a712fd991927c55f59a2ffecommit-bot@chromium.org#include "SkRect.h"
13aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
1481c3f8de1cbb93a8b99d730a75ab16d864612e95bsalomon@google.comclass GrStencilBuffer;
15aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.comclass GrTexture;
16aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
17aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com/**
18aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * GrRenderTarget represents a 2D buffer of pixels that can be rendered to.
19aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * A context's render target is set by setRenderTarget(). Render targets are
20aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * created by a createTexture with the kRenderTarget_TextureFlag flag.
21aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * Additionally, GrContext provides methods for creating GrRenderTargets
22aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * that wrap externally created render targets.
23aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com */
247d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.comclass GrRenderTarget : public GrSurface {
25aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.compublic:
264d73ac22a1b99402fc8cff78a4eb4b27aa8fe019robertphillips@google.com    SK_DECLARE_INST_COUNT(GrRenderTarget)
27558a75bcb323c03dd7482555c7214062a363276fbsalomon@google.com
287d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    // GrResource overrides
297d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    virtual size_t sizeInBytes() const SK_OVERRIDE;
307d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
317d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    // GrSurface overrides
32aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    /**
332d0baded0f45dfde9dc8c25313ff14ea18c0c915bsalomon@google.com     * @return the texture associated with the render target, may be NULL.
34aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     */
357d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    virtual GrTexture* asTexture() SK_OVERRIDE { return fTexture; }
367d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    virtual const GrTexture* asTexture() const SK_OVERRIDE { return fTexture; }
377d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
38aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    /**
397d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @return this render target.
40aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     */
417d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    virtual GrRenderTarget* asRenderTarget() SK_OVERRIDE { return this; }
42fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    virtual const GrRenderTarget* asRenderTarget() const  SK_OVERRIDE {
43fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com        return this;
447d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    }
45aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
467d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    virtual bool readPixels(int left, int top, int width, int height,
470342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                            GrPixelConfig config,
480342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                            void* buffer,
490342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                            size_t rowBytes = 0,
500342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                            uint32_t pixelOpsFlags = 0) SK_OVERRIDE;
51aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
527d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    virtual void writePixels(int left, int top, int width, int height,
530342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                             GrPixelConfig config,
540342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                             const void* buffer,
550342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                             size_t rowBytes = 0,
560342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                             uint32_t pixelOpsFlags = 0) SK_OVERRIDE;
57aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
587d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    // GrRenderTarget
59aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    /**
60aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     * If this RT is multisampled, this is the multisample buffer
61aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     * @return the 3D API's handle to this object (e.g. FBO ID in OpenGL)
62aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     */
6316e3ddea6a80972aced04b21b1d66377fa95e7c7bsalomon@google.com    virtual GrBackendObject getRenderTargetHandle() const = 0;
64aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
65aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    /**
66aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     * If this RT is multisampled, this is the buffer it is resolved to.
67aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     * Otherwise, same as getRenderTargetHandle().
682d0baded0f45dfde9dc8c25313ff14ea18c0c915bsalomon@google.com     * (In GL a separate FBO ID is used for the MSAA and resolved buffers)
69aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     * @return the 3D API's handle to this object (e.g. FBO ID in OpenGL)
70aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     */
7116e3ddea6a80972aced04b21b1d66377fa95e7c7bsalomon@google.com    virtual GrBackendObject getRenderTargetResolvedHandle() const = 0;
72aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
73aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    /**
747d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @return true if the surface is multisampled, false otherwise
75aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     */
76e98ade4fdee03a234f68669bea84d07db4a8527erobertphillips@google.com    bool isMultisampled() const { return 0 != fDesc.fSampleCnt; }
775bfc21761e0ce41206acac4c5008fc0f24debd44bsalomon@google.com
785bfc21761e0ce41206acac4c5008fc0f24debd44bsalomon@google.com    /**
795bfc21761e0ce41206acac4c5008fc0f24debd44bsalomon@google.com     * @return the number of samples-per-pixel or zero if non-MSAA.
805bfc21761e0ce41206acac4c5008fc0f24debd44bsalomon@google.com     */
81e98ade4fdee03a234f68669bea84d07db4a8527erobertphillips@google.com    int numSamples() const { return fDesc.fSampleCnt; }
82aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
83aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    /**
84aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     * Call to indicate the multisample contents were modified such that the
85aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     * render target needs to be resolved before it can be used as texture. Gr
86aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     * tracks this for its own drawing and thus this only needs to be called
8716e3ddea6a80972aced04b21b1d66377fa95e7c7bsalomon@google.com     * when the render target has been modified outside of Gr. This has no
8816e3ddea6a80972aced04b21b1d66377fa95e7c7bsalomon@google.com     * effect on wrapped backend render targets.
8916e3ddea6a80972aced04b21b1d66377fa95e7c7bsalomon@google.com     *
90aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     * @param rect  a rect bounding the area needing resolve. NULL indicates
91aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     *              the whole RT needs resolving.
92aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     */
93fd03d4a829efe2d77a712fd991927c55f59a2ffecommit-bot@chromium.org    void flagAsNeedingResolve(const SkIRect* rect = NULL);
94aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
95aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    /**
96aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     * Call to override the region that needs to be resolved.
97aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     */
98fd03d4a829efe2d77a712fd991927c55f59a2ffecommit-bot@chromium.org    void overrideResolveRect(const SkIRect rect);
99aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
100aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    /**
101aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     * Call to indicate that GrRenderTarget was externally resolved. This may
102aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     * allow Gr to skip a redundant resolve step.
103aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     */
104aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    void flagAsResolved() { fResolveRect.setLargestInverted(); }
105aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
106aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    /**
107aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     * @return true if the GrRenderTarget requires MSAA resolving
108aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     */
109aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    bool needsResolve() const { return !fResolveRect.isEmpty(); }
110aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
111aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    /**
112aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     * Returns a rect bounding the region needing resolving.
113aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     */
114fd03d4a829efe2d77a712fd991927c55f59a2ffecommit-bot@chromium.org    const SkIRect& getResolveRect() const { return fResolveRect; }
115aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
11675f9f25d8bf2adc0494f9afbbd5965809ee13acabsalomon@google.com    /**
11775f9f25d8bf2adc0494f9afbbd5965809ee13acabsalomon@google.com     * If the render target is multisampled this will perform a multisample
11875f9f25d8bf2adc0494f9afbbd5965809ee13acabsalomon@google.com     * resolve. Any pending draws to the target are first flushed. This only
11975f9f25d8bf2adc0494f9afbbd5965809ee13acabsalomon@google.com     * applies to render targets that are associated with GrTextures. After the
12075f9f25d8bf2adc0494f9afbbd5965809ee13acabsalomon@google.com     * function returns the GrTexture will contain the resolved pixels.
12175f9f25d8bf2adc0494f9afbbd5965809ee13acabsalomon@google.com     */
12275f9f25d8bf2adc0494f9afbbd5965809ee13acabsalomon@google.com    void resolve();
12375f9f25d8bf2adc0494f9afbbd5965809ee13acabsalomon@google.com
124aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    // a MSAA RT may require explicit resolving , it may auto-resolve (e.g. FBO
125fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com    // 0 in GL), or be unresolvable because the client didn't give us the
126aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    // resolve destination.
127aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    enum ResolveType {
128aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com        kCanResolve_ResolveType,
129aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com        kAutoResolves_ResolveType,
130aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com        kCantResolve_ResolveType,
131aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    };
132aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    virtual ResolveType getResolveType() const = 0;
133aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
13481c3f8de1cbb93a8b99d730a75ab16d864612e95bsalomon@google.com    /**
13581c3f8de1cbb93a8b99d730a75ab16d864612e95bsalomon@google.com     * GrStencilBuffer is not part of the public API.
13681c3f8de1cbb93a8b99d730a75ab16d864612e95bsalomon@google.com     */
13781c3f8de1cbb93a8b99d730a75ab16d864612e95bsalomon@google.com    GrStencilBuffer* getStencilBuffer() const { return fStencilBuffer; }
13881c3f8de1cbb93a8b99d730a75ab16d864612e95bsalomon@google.com    void setStencilBuffer(GrStencilBuffer* stencilBuffer);
13981c3f8de1cbb93a8b99d730a75ab16d864612e95bsalomon@google.com
140aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.comprotected:
141aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    GrRenderTarget(GrGpu* gpu,
142728302281920727b96e6cec0bfc7575900f34a8bbsalomon@google.com                   bool isWrapped,
143aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com                   GrTexture* texture,
1443cb406bb88f5aa09cf9f5a9554b4b1314cf1a2eesenorblanco@chromium.org                   const GrTextureDesc& desc)
1453cb406bb88f5aa09cf9f5a9554b4b1314cf1a2eesenorblanco@chromium.org        : INHERITED(gpu, isWrapped, desc)
14681c3f8de1cbb93a8b99d730a75ab16d864612e95bsalomon@google.com        , fStencilBuffer(NULL)
147e98ade4fdee03a234f68669bea84d07db4a8527erobertphillips@google.com        , fTexture(texture) {
148aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com        fResolveRect.setLargestInverted();
149aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    }
150aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
151d6bbbf8a831cc982cda9b91e84c5600c631af5b2robertphillips@google.com    // override of GrResource
152d6bbbf8a831cc982cda9b91e84c5600c631af5b2robertphillips@google.com    virtual void onAbandon() SK_OVERRIDE;
153d6bbbf8a831cc982cda9b91e84c5600c631af5b2robertphillips@google.com    virtual void onRelease() SK_OVERRIDE;
154d6bbbf8a831cc982cda9b91e84c5600c631af5b2robertphillips@google.com
155aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.comprivate:
156686bcb871b8425603b9accbf72e27a9309f786d8bsalomon@google.com    friend class GrTexture;
157686bcb871b8425603b9accbf72e27a9309f786d8bsalomon@google.com    // called by ~GrTexture to remove the non-ref'ed back ptr.
158686bcb871b8425603b9accbf72e27a9309f786d8bsalomon@google.com    void owningTextureDestroyed() {
159686bcb871b8425603b9accbf72e27a9309f786d8bsalomon@google.com        GrAssert(NULL != fTexture);
160686bcb871b8425603b9accbf72e27a9309f786d8bsalomon@google.com        fTexture = NULL;
161686bcb871b8425603b9accbf72e27a9309f786d8bsalomon@google.com    }
162686bcb871b8425603b9accbf72e27a9309f786d8bsalomon@google.com
163558a75bcb323c03dd7482555c7214062a363276fbsalomon@google.com    GrStencilBuffer*  fStencilBuffer;
164558a75bcb323c03dd7482555c7214062a363276fbsalomon@google.com    GrTexture*        fTexture; // not ref'ed
165e98ade4fdee03a234f68669bea84d07db4a8527erobertphillips@google.com
166fd03d4a829efe2d77a712fd991927c55f59a2ffecommit-bot@chromium.org    SkIRect           fResolveRect;
167aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
1687d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    typedef GrSurface INHERITED;
169aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com};
170aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
171aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com#endif
172