skia2/include/gpu/GrRenderTarget.h

204 lines
6.6 KiB
C
Raw Normal View History

/*
* Copyright 2011 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef GrRenderTarget_DEFINED
#define GrRenderTarget_DEFINED
#include "GrSurface.h"
#include "SkRect.h"
class GrCaps;
class GrDrawTarget;
class GrStencilAttachment;
class GrRenderTargetPriv;
/**
* GrRenderTarget represents a 2D buffer of pixels that can be rendered to.
* A context's render target is set by setRenderTarget(). Render targets are
* created by a createTexture with the kRenderTarget_SurfaceFlag flag.
* Additionally, GrContext provides methods for creating GrRenderTargets
* that wrap externally created render targets.
*/
class GrRenderTarget : virtual public GrSurface {
public:
// GrSurface overrides
GrRenderTarget* asRenderTarget() override { return this; }
const GrRenderTarget* asRenderTarget() const override { return this; }
// GrRenderTarget
/**
* On some hardware it is possible for a render target to have multisampling
* only in certain buffers.
* Enforce only two legal sample configs.
* kUnified_SampleConfig signifies multisampling in both color and stencil
* buffers and is available across all hardware.
* kStencil_SampleConfig means multisampling is present in stencil buffer
* only; this config requires hardware support of
* NV_framebuffer_mixed_samples.
*/
enum SampleConfig {
kUnified_SampleConfig = 0,
kStencil_SampleConfig = 1
};
/**
* @return true if the surface is multisampled in all buffers,
* false otherwise
*/
bool isUnifiedMultisampled() const {
if (fSampleConfig != kUnified_SampleConfig) {
return false;
}
return 0 != fDesc.fSampleCnt;
}
/**
* @return true if the surface is multisampled in the stencil buffer,
* false otherwise
*/
bool isStencilBufferMultisampled() const {
return 0 != fDesc.fSampleCnt;
}
/**
* @return the number of color samples-per-pixel, or zero if non-MSAA or
* multisampled in the stencil buffer only.
*/
int numColorSamples() const {
if (fSampleConfig == kUnified_SampleConfig) {
return fDesc.fSampleCnt;
}
return 0;
}
/**
* @return the number of stencil samples-per-pixel, or zero if non-MSAA.
*/
int numStencilSamples() const {
return fDesc.fSampleCnt;
}
/**
* @return true if the surface is mixed sampled, false otherwise.
*/
bool hasMixedSamples() const {
SkASSERT(kStencil_SampleConfig != fSampleConfig ||
this->isStencilBufferMultisampled());
return kStencil_SampleConfig == fSampleConfig;
}
/**
* Call to indicate the multisample contents were modified such that the
* render target needs to be resolved before it can be used as texture. Gr
* tracks this for its own drawing and thus this only needs to be called
* when the render target has been modified outside of Gr. This has no
* effect on wrapped backend render targets.
*
* @param rect a rect bounding the area needing resolve. NULL indicates
* the whole RT needs resolving.
*/
void flagAsNeedingResolve(const SkIRect* rect = NULL);
/**
* Call to override the region that needs to be resolved.
*/
void overrideResolveRect(const SkIRect rect);
/**
* Call to indicate that GrRenderTarget was externally resolved. This may
* allow Gr to skip a redundant resolve step.
*/
void flagAsResolved() { fResolveRect.setLargestInverted(); }
/**
* @return true if the GrRenderTarget requires MSAA resolving
*/
bool needsResolve() const { return !fResolveRect.isEmpty(); }
/**
* Returns a rect bounding the region needing resolving.
*/
const SkIRect& getResolveRect() const { return fResolveRect; }
/**
* Provide a performance hint that the render target's contents are allowed
* to become undefined.
*/
void discard();
// a MSAA RT may require explicit resolving , it may auto-resolve (e.g. FBO
// 0 in GL), or be unresolvable because the client didn't give us the
// resolve destination.
enum ResolveType {
kCanResolve_ResolveType,
kAutoResolves_ResolveType,
kCantResolve_ResolveType,
};
virtual ResolveType getResolveType() const = 0;
/**
* Return the native ID or handle to the rendertarget, depending on the
* platform. e.g. on OpenGL, return the FBO ID.
*/
virtual GrBackendObject getRenderTargetHandle() const = 0;
// Checked when this object is asked to attach a stencil buffer.
virtual bool canAttemptStencilAttachment() const = 0;
// Provides access to functions that aren't part of the public API.
GrRenderTargetPriv renderTargetPriv();
const GrRenderTargetPriv renderTargetPriv() const;
void setLastDrawTarget(GrDrawTarget* dt);
GrDrawTarget* getLastDrawTarget() { return fLastDrawTarget; }
static SampleConfig ComputeSampleConfig(const GrCaps& caps, int sampleCnt);
protected:
Refactor to separate backend object lifecycle and GpuResource budget decision Refactor GrGpuResource to contain two different pieces of state: a) instance is budgeted or not budgeted b) instance references wrapped backend objects or not The "object lifecycle" was also attached to backend object handles (ids), which made the code a bit unclear. Backend objects would be associated with GrGpuResource::LifeCycle, even though GrGpuResource::LifeCycle refers to the GpuResource, and individual backend objects in one GpuResource might be governed with different "lifecycle". Mark the budgeted/not budgeted with SkBudgeted::kYes, SkBudgeted::kNo. This was previously GrGpuResource::kCached_LifeCycle, GrGpuResource::kUncached_LifeCycle. Mark the "references wrapped object" with boolean. This was previously GrGpuResource::kBorrowed_LifeCycle, GrGpuResource::kAdopted_LifeCycle for GrGpuResource. Associate the backend object ownership status with GrBackendObjectOwnership for the backend object handles. The resource type leaf constuctors, such has GrGLTexture or GrGLTextureRenderTarget take "budgeted" parameter. This parameter is passed to GrGpuResource::registerWithCache(). The resource type intermediary constructors, such as GrGLTexture constructors for class GrGLTextureRenderTarget do not take "budgeted" parameters, intermediary construtors do not call registerWithCache. Removes the need for tagging GrGpuResource -derived subclass constructors with "Derived" parameter. Makes instances that wrap backend objects be registered with a new function GrGpuResource::registerWithCacheWrapped(). Removes "budgeted" parameter from classes such as StencilAttahment, as they are always cached and never wrap any external backend objects. Removes the use of concept "external" from the member function names. The API refers to the objects as "wrapped", so make all related functions use the term consistently. No change in functionality. Resources referencing wrapped objects are always inserted to the cache with budget decision kNo. BUG=594928 GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1862043002 Review URL: https://codereview.chromium.org/1862043002
2016-04-22 08:48:29 +00:00
GrRenderTarget(GrGpu* gpu, const GrSurfaceDesc& desc,
SampleConfig sampleConfig, GrStencilAttachment* stencil = nullptr)
Refactor to separate backend object lifecycle and GpuResource budget decision Refactor GrGpuResource to contain two different pieces of state: a) instance is budgeted or not budgeted b) instance references wrapped backend objects or not The "object lifecycle" was also attached to backend object handles (ids), which made the code a bit unclear. Backend objects would be associated with GrGpuResource::LifeCycle, even though GrGpuResource::LifeCycle refers to the GpuResource, and individual backend objects in one GpuResource might be governed with different "lifecycle". Mark the budgeted/not budgeted with SkBudgeted::kYes, SkBudgeted::kNo. This was previously GrGpuResource::kCached_LifeCycle, GrGpuResource::kUncached_LifeCycle. Mark the "references wrapped object" with boolean. This was previously GrGpuResource::kBorrowed_LifeCycle, GrGpuResource::kAdopted_LifeCycle for GrGpuResource. Associate the backend object ownership status with GrBackendObjectOwnership for the backend object handles. The resource type leaf constuctors, such has GrGLTexture or GrGLTextureRenderTarget take "budgeted" parameter. This parameter is passed to GrGpuResource::registerWithCache(). The resource type intermediary constructors, such as GrGLTexture constructors for class GrGLTextureRenderTarget do not take "budgeted" parameters, intermediary construtors do not call registerWithCache. Removes the need for tagging GrGpuResource -derived subclass constructors with "Derived" parameter. Makes instances that wrap backend objects be registered with a new function GrGpuResource::registerWithCacheWrapped(). Removes "budgeted" parameter from classes such as StencilAttahment, as they are always cached and never wrap any external backend objects. Removes the use of concept "external" from the member function names. The API refers to the objects as "wrapped", so make all related functions use the term consistently. No change in functionality. Resources referencing wrapped objects are always inserted to the cache with budget decision kNo. BUG=594928 GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1862043002 Review URL: https://codereview.chromium.org/1862043002
2016-04-22 08:48:29 +00:00
: INHERITED(gpu, desc)
, fStencilAttachment(stencil)
, fSampleConfig(sampleConfig)
, fLastDrawTarget(nullptr) {
fResolveRect.setLargestInverted();
}
~GrRenderTarget() override;
// override of GrResource
void onAbandon() override;
void onRelease() override;
private:
// Allows the backends to perform any additional work that is required for attaching a
// GrStencilAttachment. When this is called, the GrStencilAttachment has already been put onto
// the GrRenderTarget. This function must return false if any failures occur when completing the
// stencil attachment.
virtual bool completeStencilAttachment() = 0;
friend class GrRenderTargetPriv;
GrStencilAttachment* fStencilAttachment;
SampleConfig fSampleConfig;
SkIRect fResolveRect;
// The last drawTarget that wrote to or is currently going to write to this renderTarget
// The drawTarget can be closed (e.g., no draw context is currently bound
// to this renderTarget).
// This back-pointer is required so that we can add a dependancy between
// the drawTarget used to create the current contents of this renderTarget
// and the drawTarget of a destination renderTarget to which this one is being drawn.
GrDrawTarget* fLastDrawTarget;
typedef GrSurface INHERITED;
};
#endif