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 (NULL != 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 {
30 if (this->isSetFlag((GrTextureFlags) kReturnToCache_FlagBit) &&
31 NULL != this->INHERITED::getContext()) {
32 GrTexture* nonConstThis = const_cast<GrTexture *>(this);
33 this->fRefCnt = 1; // restore ref count to initial setting
35 nonConstThis->resetFlag((GrTextureFlags) kReturnToCache_FlagBit);
36 nonConstThis->INHERITED::getContext()->addExistingTextureToCache(nonConstThis);
38 // Note: "this" texture might be freed inside addExistingTextureToCache
43 SkASSERT(0 == this->getDeferredRefCount());
44 this->INHERITED::internal_dispose();
47 void GrTexture::dirtyMipMaps(bool mipMapsDirty) {
49 if (kValid_MipMapsStatus == fMipMapsStatus) {
50 fMipMapsStatus = kAllocated_MipMapsStatus;
53 const bool sizeChanged = kNotAllocated_MipMapsStatus == fMipMapsStatus;
54 fMipMapsStatus = kValid_MipMapsStatus;
56 // This must not be called until after changing fMipMapsStatus.
57 this->didChangeGpuMemorySize();
62 size_t GrTexture::gpuMemorySize() const {
63 size_t textureSize = (size_t) fDesc.fWidth *
65 GrBytesPerPixel(fDesc.fConfig);
66 if (kNotAllocated_MipMapsStatus != fMipMapsStatus) {
67 // We don't have to worry about the mipmaps being a different size than
68 // we'd expect because we never change fDesc.fWidth/fHeight.
74 bool GrTexture::readPixels(int left, int top, int width, int height,
75 GrPixelConfig config, void* buffer,
76 size_t rowBytes, uint32_t pixelOpsFlags) {
77 // go through context so that all necessary flushing occurs
78 GrContext* context = this->getContext();
79 if (NULL == context) {
82 return context->readTexturePixels(this,
83 left, top, width, height,
84 config, buffer, rowBytes,
88 void GrTexture::writePixels(int left, int top, int width, int height,
89 GrPixelConfig config, const void* buffer,
90 size_t rowBytes, uint32_t pixelOpsFlags) {
91 // go through context so that all necessary flushing occurs
92 GrContext* context = this->getContext();
93 if (NULL == context) {
96 context->writeTexturePixels(this,
97 left, top, width, height,
98 config, buffer, rowBytes,
102 void GrTexture::onRelease() {
103 SkASSERT(!this->isSetFlag((GrTextureFlags) kReturnToCache_FlagBit));
104 INHERITED::onRelease();
107 void GrTexture::onAbandon() {
108 if (NULL != fRenderTarget.get()) {
109 fRenderTarget->abandon();
111 INHERITED::onAbandon();
114 void GrTexture::validateDesc() const {
115 if (NULL != this->asRenderTarget()) {
116 // This texture has a render target
117 SkASSERT(0 != (fDesc.fFlags & kRenderTarget_GrTextureFlagBit));
119 if (NULL != this->asRenderTarget()->getStencilBuffer()) {
120 SkASSERT(0 != (fDesc.fFlags & kNoStencil_GrTextureFlagBit));
122 SkASSERT(0 == (fDesc.fFlags & kNoStencil_GrTextureFlagBit));
125 SkASSERT(fDesc.fSampleCnt == this->asRenderTarget()->numSamples());
127 SkASSERT(0 == (fDesc.fFlags & kRenderTarget_GrTextureFlagBit));
128 SkASSERT(0 == (fDesc.fFlags & kNoStencil_GrTextureFlagBit));
129 SkASSERT(0 == fDesc.fSampleCnt);
133 // These flags need to fit in a GrResourceKey::ResourceFlags so they can be folded into the texture
137 * The kStretchToPOT bit is set when the texture is NPOT and is being repeated but the
138 * hardware doesn't support that feature.
140 kStretchToPOT_TextureFlag = 0x1,
142 * The kBilerp bit can only be set when the kStretchToPOT flag is set and indicates whether the
143 * stretched texture should be bilerped.
145 kBilerp_TextureFlag = 0x2,
149 GrResourceKey::ResourceFlags get_texture_flags(const GrGpu* gpu,
150 const GrTextureParams* params,
151 const GrTextureDesc& desc) {
152 GrResourceKey::ResourceFlags flags = 0;
153 bool tiled = NULL != params && params->isTiled();
154 if (tiled && !gpu->caps()->npotTextureTileSupport()) {
155 if (!GrIsPow2(desc.fWidth) || !GrIsPow2(desc.fHeight)) {
156 flags |= kStretchToPOT_TextureFlag;
157 switch(params->filterMode()) {
158 case GrTextureParams::kNone_FilterMode:
160 case GrTextureParams::kBilerp_FilterMode:
161 case GrTextureParams::kMipMap_FilterMode:
162 flags |= kBilerp_TextureFlag;
170 GrResourceKey::ResourceType texture_resource_type() {
171 static const GrResourceKey::ResourceType gType = GrResourceKey::GenerateResourceType();
175 // FIXME: This should be refactored with the code in gl/GrGpuGL.cpp.
176 GrSurfaceOrigin resolve_origin(const GrTextureDesc& desc) {
177 // By default, GrRenderTargets are GL's normal orientation so that they
178 // can be drawn to by the outside world without the client having
179 // to render upside down.
180 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
181 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
182 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
189 GrResourceKey GrTexture::ComputeKey(const GrGpu* gpu,
190 const GrTextureParams* params,
191 const GrTextureDesc& desc,
192 const GrCacheID& cacheID) {
193 GrResourceKey::ResourceFlags flags = get_texture_flags(gpu, params, desc);
194 return GrResourceKey(cacheID, texture_resource_type(), flags);
197 GrResourceKey GrTexture::ComputeScratchKey(const GrTextureDesc& desc) {
198 GrCacheID::Key idKey;
199 // Instead of a client-provided key of the texture contents we create a key from the
201 GR_STATIC_ASSERT(sizeof(idKey) >= 16);
202 SkASSERT(desc.fHeight < (1 << 16));
203 SkASSERT(desc.fWidth < (1 << 16));
204 idKey.fData32[0] = (desc.fWidth) | (desc.fHeight << 16);
205 idKey.fData32[1] = desc.fConfig | desc.fSampleCnt << 16;
206 idKey.fData32[2] = desc.fFlags;
207 idKey.fData32[3] = resolve_origin(desc); // Only needs 2 bits actually
208 static const int kPadSize = sizeof(idKey) - 16;
209 GR_STATIC_ASSERT(kPadSize >= 0);
210 memset(idKey.fData8 + 16, 0, kPadSize);
212 GrCacheID cacheID(GrResourceKey::ScratchDomain(), idKey);
213 return GrResourceKey(cacheID, texture_resource_type(), 0);
216 bool GrTexture::NeedsResizing(const GrResourceKey& key) {
217 return SkToBool(key.getResourceFlags() & kStretchToPOT_TextureFlag);
220 bool GrTexture::NeedsBilerp(const GrResourceKey& key) {
221 return SkToBool(key.getResourceFlags() & kBilerp_TextureFlag);