GrRenderTarget.h revision 81c3f8de1cbb93a8b99d730a75ab16d864612e95
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" 22aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com#include "GrResource.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 */ 34aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.comclass GrRenderTarget : public GrResource { 35aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com 36aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.compublic: 37aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com /** 38aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * @return the width of the rendertarget 39aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com */ 40aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com int width() const { return fWidth; } 41aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com /** 42aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * @return the height of the rendertarget 43aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com */ 44aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com int height() const { return fHeight; } 45aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com 46aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com /** 47aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * @return the pixel config. Can be kUnknown_GrPixelConfig 48aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * if client asked us to render to a target that has a pixel 49aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * config that isn't equivalent with one of our configs. 50aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com */ 51aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com int config() const { return fConfig; } 52aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com 53aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com /** 54aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * @return the texture associated with the rendertarget, may be NULL. 55aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com */ 56aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com GrTexture* asTexture() {return fTexture;} 57aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com 58aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com /** 59aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * If this RT is multisampled, this is the multisample buffer 60aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * @return the 3D API's handle to this object (e.g. FBO ID in OpenGL) 61aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com */ 62aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com virtual intptr_t getRenderTargetHandle() const = 0; 63aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com 64aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com /** 65aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * If this RT is multisampled, this is the buffer it is resolved to. 66aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * Otherwise, same as getRenderTargetHandle(). 67aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * (In GL a separate FBO ID is used for the msaa and resolved buffers) 68aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * @return the 3D API's handle to this object (e.g. FBO ID in OpenGL) 69aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com */ 70aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com virtual intptr_t getRenderTargetResolvedHandle() const = 0; 71aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com 72aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com /** 73aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * @return true if the render target is multisampled, false otherwise 74aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com */ 755bfc21761e0ce41206acac4c5008fc0f24debd44bsalomon@google.com bool isMultisampled() const { return 0 != fSampleCnt; } 765bfc21761e0ce41206acac4c5008fc0f24debd44bsalomon@google.com 775bfc21761e0ce41206acac4c5008fc0f24debd44bsalomon@google.com /** 785bfc21761e0ce41206acac4c5008fc0f24debd44bsalomon@google.com * @return the number of samples-per-pixel or zero if non-MSAA. 795bfc21761e0ce41206acac4c5008fc0f24debd44bsalomon@google.com */ 805bfc21761e0ce41206acac4c5008fc0f24debd44bsalomon@google.com int numSamples() const { return fSampleCnt; } 81aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com 82aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com /** 83aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * Call to indicate the multisample contents were modified such that the 84aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * render target needs to be resolved before it can be used as texture. Gr 85aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * tracks this for its own drawing and thus this only needs to be called 86aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * when the render target has been modified outside of Gr. Only meaningful 87aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * for Gr-created RT/Textures and Platform RT/Textures created with the 88aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * kGrCanResolve flag. 89aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * @param rect a rect bounding the area needing resolve. NULL indicates 90aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * the whole RT needs resolving. 91aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com */ 92aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com void flagAsNeedingResolve(const GrIRect* rect = NULL); 93aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com 94aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com /** 95aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * Call to override the region that needs to be resolved. 96aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com */ 97aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com void overrideResolveRect(const GrIRect rect); 98aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com 99aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com /** 100aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * Call to indicate that GrRenderTarget was externally resolved. This may 101aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * allow Gr to skip a redundant resolve step. 102aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com */ 103aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com void flagAsResolved() { fResolveRect.setLargestInverted(); } 104aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com 105aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com /** 106aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * @return true if the GrRenderTarget requires MSAA resolving 107aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com */ 108aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com bool needsResolve() const { return !fResolveRect.isEmpty(); } 109aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com 110aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com /** 111aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * Returns a rect bounding the region needing resolving. 112aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com */ 113aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com const GrIRect& getResolveRect() const { return fResolveRect; } 114aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com 115aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com // GrResource overrides 116aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com virtual size_t sizeInBytes() const; 117aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com 118aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com /** 119aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * Reads a rectangle of pixels from the render target. 120aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * @param left left edge of the rectangle to read (inclusive) 121aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * @param top top edge of the rectangle to read (inclusive) 122aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * @param width width of rectangle to read in pixels. 123aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * @param height height of rectangle to read in pixels. 124aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * @param config the pixel config of the destination buffer 125aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * @param buffer memory to read the rectangle into. 126aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * 127aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * @return true if the read succeeded, false if not. The read can fail 128aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * because of a unsupported pixel config. 129aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com */ 130aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com bool readPixels(int left, int top, int width, int height, 131aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com GrPixelConfig config, void* buffer); 132aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com 133aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com // a MSAA RT may require explicit resolving , it may auto-resolve (e.g. FBO 134aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com // 0 in GL), or be unresolvable because the client didn't give us the 135aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com // resolve destination. 136aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com enum ResolveType { 137aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com kCanResolve_ResolveType, 138aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com kAutoResolves_ResolveType, 139aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com kCantResolve_ResolveType, 140aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com }; 141aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com virtual ResolveType getResolveType() const = 0; 142aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com 14381c3f8de1cbb93a8b99d730a75ab16d864612e95bsalomon@google.com /** 14481c3f8de1cbb93a8b99d730a75ab16d864612e95bsalomon@google.com * GrStencilBuffer is not part of the public API. 14581c3f8de1cbb93a8b99d730a75ab16d864612e95bsalomon@google.com */ 14681c3f8de1cbb93a8b99d730a75ab16d864612e95bsalomon@google.com GrStencilBuffer* getStencilBuffer() const { return fStencilBuffer; } 14781c3f8de1cbb93a8b99d730a75ab16d864612e95bsalomon@google.com void setStencilBuffer(GrStencilBuffer* stencilBuffer); 14881c3f8de1cbb93a8b99d730a75ab16d864612e95bsalomon@google.com 149aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.comprotected: 150aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com GrRenderTarget(GrGpu* gpu, 151aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com GrTexture* texture, 152aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com int width, 153aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com int height, 154aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com GrPixelConfig config, 1555bfc21761e0ce41206acac4c5008fc0f24debd44bsalomon@google.com int sampleCnt) 156aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com : INHERITED(gpu) 15781c3f8de1cbb93a8b99d730a75ab16d864612e95bsalomon@google.com , fStencilBuffer(NULL) 158aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com , fTexture(texture) 159aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com , fWidth(width) 160aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com , fHeight(height) 161aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com , fConfig(config) 1625bfc21761e0ce41206acac4c5008fc0f24debd44bsalomon@google.com , fSampleCnt(sampleCnt) 163aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com { 164aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com fResolveRect.setLargestInverted(); 165aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com } 166aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com 167aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com friend class GrTexture; 168aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com // When a texture unrefs an owned rendertarget this func 169aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com // removes the back pointer. This could be done called from 170aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com // texture's destructor but would have to be done in derived 171aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com // class. By the time of texture base destructor it has already 172aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com // lost its pointer to the rt. 173aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com void onTextureReleaseRenderTarget() { 174aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com GrAssert(NULL != fTexture); 175aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com fTexture = NULL; 176aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com } 177aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com 17881c3f8de1cbb93a8b99d730a75ab16d864612e95bsalomon@google.com GrStencilBuffer* fStencilBuffer; 17981c3f8de1cbb93a8b99d730a75ab16d864612e95bsalomon@google.com 180aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.comprivate: 18181c3f8de1cbb93a8b99d730a75ab16d864612e95bsalomon@google.com GrTexture* fTexture; // not ref'ed 182aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com int fWidth; 183aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com int fHeight; 184aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com GrPixelConfig fConfig; 1855bfc21761e0ce41206acac4c5008fc0f24debd44bsalomon@google.com int fSampleCnt; 186aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com GrIRect fResolveRect; 187aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com 188aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com typedef GrResource INHERITED; 189aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com}; 190aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com 191aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com#endif 192