GrRenderTarget.h revision 7d501ab502e861f2b2367b10a701b8449241558e
1aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com/*
2aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    Copyright 2011 Google Inc.
3aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
4aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    Licensed under the Apache License, Version 2.0 (the "License");
5aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    you may not use this file except in compliance with the License.
6aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    You may obtain a copy of the License at
7aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
8aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com         http://www.apache.org/licenses/LICENSE-2.0
9aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
10aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    Unless required by applicable law or agreed to in writing, software
11aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    distributed under the License is distributed on an "AS IS" BASIS,
12aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    See the License for the specific language governing permissions and
14aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    limitations under the License.
15aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com */
16aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
17aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
18aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com#ifndef GrRenderTarget_DEFINED
19aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com#define GrRenderTarget_DEFINED
20aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
21aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com#include "GrRect.h"
227d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com#include "GrSurface.h"
23aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
2481c3f8de1cbb93a8b99d730a75ab16d864612e95bsalomon@google.comclass GrStencilBuffer;
25aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.comclass GrTexture;
26aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
27aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com/**
28aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * GrRenderTarget represents a 2D buffer of pixels that can be rendered to.
29aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * A context's render target is set by setRenderTarget(). Render targets are
30aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * created by a createTexture with the kRenderTarget_TextureFlag flag.
31aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * Additionally, GrContext provides methods for creating GrRenderTargets
32aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * that wrap externally created render targets.
33aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com */
347d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.comclass GrRenderTarget : public GrSurface {
35aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.compublic:
364d73ac22a1b99402fc8cff78a4eb4b27aa8fe019robertphillips@google.com    SK_DECLARE_INST_COUNT(GrRenderTarget)
37558a75bcb323c03dd7482555c7214062a363276fbsalomon@google.com
387d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    // GrResource overrides
397d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    virtual size_t sizeInBytes() const SK_OVERRIDE;
407d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
417d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    // GrSurface overrides
42aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    /**
437d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @return the texture associated with the rendertarget, may be NULL.
44aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     */
457d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    virtual GrTexture* asTexture() SK_OVERRIDE { return fTexture; }
467d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    virtual const GrTexture* asTexture() const SK_OVERRIDE { return fTexture; }
477d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
48aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    /**
497d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @return this render target.
50aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     */
517d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    virtual GrRenderTarget* asRenderTarget() SK_OVERRIDE { return this; }
527d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    virtual const GrRenderTarget* asRenderTarget() const  SK_OVERRIDE {
537d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com        return this;
547d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    }
55aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
56aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    /**
577d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * Reads a rectangle of pixels from the render target.
587d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param left          left edge of the rectangle to read (inclusive)
597d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param top           top edge of the rectangle to read (inclusive)
607d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param width         width of rectangle to read in pixels.
617d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param height        height of rectangle to read in pixels.
627d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param config        the pixel config of the destination buffer
637d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param buffer        memory to read the rectangle into.
647d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param rowBytes      number of bytes bewtween consecutive rows. Zero
657d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     *                      means rows are tightly packed.
667d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     *
677d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @return true if the read succeeded, false if not. The read can fail
687d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     *              because of an unsupported pixel config.
69aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     */
707d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    virtual bool readPixels(int left, int top, int width, int height,
717d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com                            GrPixelConfig config, void* buffer,
727d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com                            size_t rowBytes) SK_OVERRIDE;
73aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
74aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    /**
757d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * Copy the src pixels [buffer, rowbytes, pixelconfig] into the render
767d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * target at the specified rectangle.
777d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param left          left edge of the rectangle to write (inclusive)
787d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param top           top edge of the rectangle to write (inclusive)
797d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param width         width of rectangle to write in pixels.
807d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param height        height of rectangle to write in pixels.
817d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param config        the pixel config of the source buffer
827d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param buffer        memory to read the rectangle from.
837d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param rowBytes      number of bytes bewtween consecutive rows. Zero
847d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     *                      means rows are tightly packed.
85aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     */
867d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    virtual void writePixels(int left, int top, int width, int height,
877d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com                             GrPixelConfig config, const void* buffer,
887d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com                             size_t rowBytes) SK_OVERRIDE;
89aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
907d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    // GrRenderTarget
91aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    /**
92aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     * If this RT is multisampled, this is the multisample buffer
93aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     * @return the 3D API's handle to this object (e.g. FBO ID in OpenGL)
94aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     */
95aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    virtual intptr_t getRenderTargetHandle() const = 0;
96aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
97aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    /**
98aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     * If this RT is multisampled, this is the buffer it is resolved to.
99aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     * Otherwise, same as getRenderTargetHandle().
100aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     * (In GL a separate FBO ID is used for the msaa and resolved buffers)
101aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     * @return the 3D API's handle to this object (e.g. FBO ID in OpenGL)
102aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     */
103aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    virtual intptr_t getRenderTargetResolvedHandle() const = 0;
104aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
105aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    /**
1067d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @return true if the surface is multisampled, false otherwise
107aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     */
108e98ade4fdee03a234f68669bea84d07db4a8527erobertphillips@google.com    bool isMultisampled() const { return 0 != fDesc.fSampleCnt; }
1095bfc21761e0ce41206acac4c5008fc0f24debd44bsalomon@google.com
1105bfc21761e0ce41206acac4c5008fc0f24debd44bsalomon@google.com    /**
1115bfc21761e0ce41206acac4c5008fc0f24debd44bsalomon@google.com     * @return the number of samples-per-pixel or zero if non-MSAA.
1125bfc21761e0ce41206acac4c5008fc0f24debd44bsalomon@google.com     */
113e98ade4fdee03a234f68669bea84d07db4a8527erobertphillips@google.com    int numSamples() const { return fDesc.fSampleCnt; }
114aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
115aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    /**
116aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     * Call to indicate the multisample contents were modified such that the
117aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     * render target needs to be resolved before it can be used as texture. Gr
118aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     * tracks this for its own drawing and thus this only needs to be called
119aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     * when the render target has been modified outside of Gr. Only meaningful
120aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     * for Gr-created RT/Textures and Platform RT/Textures created with the
121aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     * kGrCanResolve flag.
122aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     * @param rect  a rect bounding the area needing resolve. NULL indicates
123aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     *              the whole RT needs resolving.
124aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     */
125aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    void flagAsNeedingResolve(const GrIRect* rect = NULL);
126aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
127aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    /**
128aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     * Call to override the region that needs to be resolved.
129aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     */
130aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    void overrideResolveRect(const GrIRect rect);
131aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
132aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    /**
133aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     * Call to indicate that GrRenderTarget was externally resolved. This may
134aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     * allow Gr to skip a redundant resolve step.
135aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     */
136aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    void flagAsResolved() { fResolveRect.setLargestInverted(); }
137aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
138aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    /**
139aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     * @return true if the GrRenderTarget requires MSAA resolving
140aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     */
141aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    bool needsResolve() const { return !fResolveRect.isEmpty(); }
142aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
143aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    /**
144aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     * Returns a rect bounding the region needing resolving.
145aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com     */
146aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    const GrIRect& getResolveRect() const { return fResolveRect; }
147aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
14875f9f25d8bf2adc0494f9afbbd5965809ee13acabsalomon@google.com    /**
14975f9f25d8bf2adc0494f9afbbd5965809ee13acabsalomon@google.com     * If the render target is multisampled this will perform a multisample
15075f9f25d8bf2adc0494f9afbbd5965809ee13acabsalomon@google.com     * resolve. Any pending draws to the target are first flushed. This only
15175f9f25d8bf2adc0494f9afbbd5965809ee13acabsalomon@google.com     * applies to render targets that are associated with GrTextures. After the
15275f9f25d8bf2adc0494f9afbbd5965809ee13acabsalomon@google.com     * function returns the GrTexture will contain the resolved pixels.
15375f9f25d8bf2adc0494f9afbbd5965809ee13acabsalomon@google.com     */
15475f9f25d8bf2adc0494f9afbbd5965809ee13acabsalomon@google.com    void resolve();
15575f9f25d8bf2adc0494f9afbbd5965809ee13acabsalomon@google.com
156aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    // a MSAA RT may require explicit resolving , it may auto-resolve (e.g. FBO
157aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    // 0 in GL), or be unresolvable because the client didn't give us the
158aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    // resolve destination.
159aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    enum ResolveType {
160aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com        kCanResolve_ResolveType,
161aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com        kAutoResolves_ResolveType,
162aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com        kCantResolve_ResolveType,
163aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    };
164aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    virtual ResolveType getResolveType() const = 0;
165aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
16681c3f8de1cbb93a8b99d730a75ab16d864612e95bsalomon@google.com    /**
16781c3f8de1cbb93a8b99d730a75ab16d864612e95bsalomon@google.com     * GrStencilBuffer is not part of the public API.
16881c3f8de1cbb93a8b99d730a75ab16d864612e95bsalomon@google.com     */
16981c3f8de1cbb93a8b99d730a75ab16d864612e95bsalomon@google.com    GrStencilBuffer* getStencilBuffer() const { return fStencilBuffer; }
17081c3f8de1cbb93a8b99d730a75ab16d864612e95bsalomon@google.com    void setStencilBuffer(GrStencilBuffer* stencilBuffer);
17181c3f8de1cbb93a8b99d730a75ab16d864612e95bsalomon@google.com
172aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.comprotected:
173aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    GrRenderTarget(GrGpu* gpu,
174aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com                   GrTexture* texture,
1757d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com                   const GrTextureDesc& desc)
1767d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com        : INHERITED(gpu, desc)
17781c3f8de1cbb93a8b99d730a75ab16d864612e95bsalomon@google.com        , fStencilBuffer(NULL)
178e98ade4fdee03a234f68669bea84d07db4a8527erobertphillips@google.com        , fTexture(texture) {
179aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com        fResolveRect.setLargestInverted();
180aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    }
181aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
182aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    friend class GrTexture;
183aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    // When a texture unrefs an owned rendertarget this func
1847d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    // removes the back pointer. This could be called from
185aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    // texture's destructor but would have to be done in derived
1867d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    // classes. By the time of texture base destructor it has already
187aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    // lost its pointer to the rt.
188aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    void onTextureReleaseRenderTarget() {
189aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com        GrAssert(NULL != fTexture);
190aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com        fTexture = NULL;
191aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    }
192aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
193aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.comprivate:
194558a75bcb323c03dd7482555c7214062a363276fbsalomon@google.com    GrStencilBuffer*  fStencilBuffer;
195558a75bcb323c03dd7482555c7214062a363276fbsalomon@google.com    GrTexture*        fTexture; // not ref'ed
196e98ade4fdee03a234f68669bea84d07db4a8527erobertphillips@google.com
197558a75bcb323c03dd7482555c7214062a363276fbsalomon@google.com    GrIRect           fResolveRect;
198aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
1997d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    typedef GrSurface INHERITED;
200aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com};
201aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
202aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com#endif
203