3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
10 #include "GrTexture.h"
12 #include "GrContext.h"
13 #include "GrDrawTargetCaps.h"
15 #include "GrRenderTarget.h"
16 #include "GrResourceCache.h"
18 GrTexture::~GrTexture() {
19 if (fRenderTarget.get()) {
20 fRenderTarget.get()->owningTextureDestroyed();
25 * This method allows us to interrupt the normal deletion process and place
26 * textures back in the texture cache when their ref count goes to zero.
28 void GrTexture::internal_dispose() const {
29 if (this->impl()->isSetFlag((GrTextureFlags) GrTextureImpl::kReturnToCache_FlagBit) &&
30 this->INHERITED::getContext()) {
31 GrTexture* nonConstThis = const_cast<GrTexture *>(this);
32 this->ref(); // restore ref count to initial setting
34 nonConstThis->impl()->resetFlag((GrTextureFlags) GrTextureImpl::kReturnToCache_FlagBit);
35 nonConstThis->INHERITED::getContext()->addExistingTextureToCache(nonConstThis);
37 // Note: "this" texture might be freed inside addExistingTextureToCache
42 this->INHERITED::internal_dispose();
45 void GrTextureImpl::dirtyMipMaps(bool mipMapsDirty) {
47 if (kValid_MipMapsStatus == fMipMapsStatus) {
48 fMipMapsStatus = kAllocated_MipMapsStatus;
51 const bool sizeChanged = kNotAllocated_MipMapsStatus == fMipMapsStatus;
52 fMipMapsStatus = kValid_MipMapsStatus;
54 // This must not be called until after changing fMipMapsStatus.
55 this->didChangeGpuMemorySize();
60 size_t GrTexture::gpuMemorySize() const {
63 if (GrPixelConfigIsCompressed(fDesc.fConfig)) {
64 textureSize = GrCompressedFormatDataSize(fDesc.fConfig, fDesc.fWidth, fDesc.fHeight);
66 textureSize = (size_t) fDesc.fWidth * fDesc.fHeight * GrBytesPerPixel(fDesc.fConfig);
69 if (this->impl()->hasMipMaps()) {
70 // We don't have to worry about the mipmaps being a different size than
71 // we'd expect because we never change fDesc.fWidth/fHeight.
77 bool GrTexture::readPixels(int left, int top, int width, int height,
78 GrPixelConfig config, void* buffer,
79 size_t rowBytes, uint32_t pixelOpsFlags) {
80 // go through context so that all necessary flushing occurs
81 GrContext* context = this->getContext();
82 if (NULL == context) {
85 return context->readTexturePixels(this,
86 left, top, width, height,
87 config, buffer, rowBytes,
91 void GrTexture::writePixels(int left, int top, int width, int height,
92 GrPixelConfig config, const void* buffer,
93 size_t rowBytes, uint32_t pixelOpsFlags) {
94 // go through context so that all necessary flushing occurs
95 GrContext* context = this->getContext();
96 if (NULL == context) {
99 context->writeTexturePixels(this,
100 left, top, width, height,
101 config, buffer, rowBytes,
105 void GrTexture::abandonReleaseCommon() {
106 // In debug builds the resource cache tracks removed/exclusive textures and has an unref'ed ptr.
107 // After abandon() or release() the resource cache will be unreachable (getContext() == NULL).
108 // So we readd the texture to the cache here so that it is removed from the exclusive list and
109 // there is no longer an unref'ed ptr to the texture in the cache.
110 if (this->impl()->isSetFlag((GrTextureFlags)GrTextureImpl::kReturnToCache_FlagBit)) {
111 SkASSERT(!this->wasDestroyed());
112 this->ref(); // restores the ref the resource cache gave up when it marked this exclusive.
113 this->impl()->resetFlag((GrTextureFlags) GrTextureImpl::kReturnToCache_FlagBit);
114 this->getContext()->addExistingTextureToCache(this);
118 void GrTexture::onRelease() {
119 this->abandonReleaseCommon();
120 SkASSERT(!this->impl()->isSetFlag((GrTextureFlags) GrTextureImpl::kReturnToCache_FlagBit));
121 INHERITED::onRelease();
124 void GrTexture::onAbandon() {
125 this->abandonReleaseCommon();
126 if (fRenderTarget.get()) {
127 fRenderTarget->abandon();
129 INHERITED::onAbandon();
132 void GrTexture::validateDesc() const {
133 if (this->asRenderTarget()) {
134 // This texture has a render target
135 SkASSERT(0 != (fDesc.fFlags & kRenderTarget_GrTextureFlagBit));
137 if (this->asRenderTarget()->getStencilBuffer()) {
138 SkASSERT(0 != (fDesc.fFlags & kNoStencil_GrTextureFlagBit));
140 SkASSERT(0 == (fDesc.fFlags & kNoStencil_GrTextureFlagBit));
143 SkASSERT(fDesc.fSampleCnt == this->asRenderTarget()->numSamples());
145 SkASSERT(0 == (fDesc.fFlags & kRenderTarget_GrTextureFlagBit));
146 SkASSERT(0 == (fDesc.fFlags & kNoStencil_GrTextureFlagBit));
147 SkASSERT(0 == fDesc.fSampleCnt);
151 //////////////////////////////////////////////////////////////////////////////
153 // These flags need to fit in a GrResourceKey::ResourceFlags so they can be folded into the texture
157 * The kStretchToPOT bit is set when the texture is NPOT and is being repeated but the
158 * hardware doesn't support that feature.
160 kStretchToPOT_TextureFlag = 0x1,
162 * The kBilerp bit can only be set when the kStretchToPOT flag is set and indicates whether the
163 * stretched texture should be bilerped.
165 kBilerp_TextureFlag = 0x2,
169 GrResourceKey::ResourceFlags get_texture_flags(const GrGpu* gpu,
170 const GrTextureParams* params,
171 const GrTextureDesc& desc) {
172 GrResourceKey::ResourceFlags flags = 0;
173 bool tiled = params && params->isTiled();
174 if (tiled && !gpu->caps()->npotTextureTileSupport()) {
175 if (!SkIsPow2(desc.fWidth) || !SkIsPow2(desc.fHeight)) {
176 flags |= kStretchToPOT_TextureFlag;
177 switch(params->filterMode()) {
178 case GrTextureParams::kNone_FilterMode:
180 case GrTextureParams::kBilerp_FilterMode:
181 case GrTextureParams::kMipMap_FilterMode:
182 flags |= kBilerp_TextureFlag;
190 GrResourceKey::ResourceType texture_resource_type() {
191 static const GrResourceKey::ResourceType gType = GrResourceKey::GenerateResourceType();
195 // FIXME: This should be refactored with the code in gl/GrGpuGL.cpp.
196 GrSurfaceOrigin resolve_origin(const GrTextureDesc& desc) {
197 // By default, GrRenderTargets are GL's normal orientation so that they
198 // can be drawn to by the outside world without the client having
199 // to render upside down.
200 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
201 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
202 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
209 //////////////////////////////////////////////////////////////////////////////
210 GrTextureImpl::GrTextureImpl(GrGpu* gpu, bool isWrapped, const GrTextureDesc& desc)
211 : INHERITED(gpu, isWrapped, desc)
212 , fMipMapsStatus(kNotAllocated_MipMapsStatus) {
213 this->setScratchKey(ComputeScratchKey(desc));
216 GrResourceKey GrTextureImpl::ComputeKey(const GrGpu* gpu,
217 const GrTextureParams* params,
218 const GrTextureDesc& desc,
219 const GrCacheID& cacheID) {
220 GrResourceKey::ResourceFlags flags = get_texture_flags(gpu, params, desc);
221 return GrResourceKey(cacheID, texture_resource_type(), flags);
224 GrResourceKey GrTextureImpl::ComputeScratchKey(const GrTextureDesc& desc) {
225 GrCacheID::Key idKey;
226 // Instead of a client-provided key of the texture contents we create a key from the
228 GR_STATIC_ASSERT(sizeof(idKey) >= 16);
229 SkASSERT(desc.fHeight < (1 << 16));
230 SkASSERT(desc.fWidth < (1 << 16));
231 idKey.fData32[0] = (desc.fWidth) | (desc.fHeight << 16);
232 idKey.fData32[1] = desc.fConfig | desc.fSampleCnt << 16;
233 idKey.fData32[2] = desc.fFlags;
234 idKey.fData32[3] = resolve_origin(desc); // Only needs 2 bits actually
235 static const int kPadSize = sizeof(idKey) - 16;
236 GR_STATIC_ASSERT(kPadSize >= 0);
237 memset(idKey.fData8 + 16, 0, kPadSize);
239 GrCacheID cacheID(GrResourceKey::ScratchDomain(), idKey);
240 return GrResourceKey(cacheID, texture_resource_type(), 0);
243 bool GrTextureImpl::NeedsResizing(const GrResourceKey& key) {
244 return SkToBool(key.getResourceFlags() & kStretchToPOT_TextureFlag);
247 bool GrTextureImpl::NeedsBilerp(const GrResourceKey& key) {
248 return SkToBool(key.getResourceFlags() & kBilerp_TextureFlag);