Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / src / gpu / GrTexture.cpp
1
2 /*
3  * Copyright 2011 Google Inc.
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8
9
10 #include "GrTexture.h"
11
12 #include "GrContext.h"
13 #include "GrDrawTargetCaps.h"
14 #include "GrGpu.h"
15 #include "GrRenderTarget.h"
16 #include "GrResourceCache.h"
17
18 GrTexture::~GrTexture() {
19     if (NULL != fRenderTarget.get()) {
20         fRenderTarget.get()->owningTextureDestroyed();
21     }
22 }
23
24 /**
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.
27  */
28 void GrTexture::internal_dispose() const {
29
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
34
35         nonConstThis->resetFlag((GrTextureFlags) kReturnToCache_FlagBit);
36         nonConstThis->INHERITED::getContext()->addExistingTextureToCache(nonConstThis);
37
38         // Note: "this" texture might be freed inside addExistingTextureToCache
39         // if it is purged.
40         return;
41     }
42
43     SkASSERT(0 == this->getDeferredRefCount());
44     this->INHERITED::internal_dispose();
45 }
46
47 void GrTexture::dirtyMipMaps(bool mipMapsDirty) {
48     if (mipMapsDirty) {
49         if (kValid_MipMapsStatus == fMipMapsStatus) {
50             fMipMapsStatus = kAllocated_MipMapsStatus;
51         }
52     } else {
53         const bool sizeChanged = kNotAllocated_MipMapsStatus == fMipMapsStatus;
54         fMipMapsStatus = kValid_MipMapsStatus;
55         if (sizeChanged) {
56             // This must not be called until after changing fMipMapsStatus.
57             this->didChangeGpuMemorySize();
58         }
59     }
60 }
61
62 size_t GrTexture::gpuMemorySize() const {
63     size_t textureSize =  (size_t) fDesc.fWidth *
64                                    fDesc.fHeight *
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.
69         textureSize *= 2;
70     }
71     return textureSize;
72 }
73
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) {
80         return false;
81     }
82     return context->readTexturePixels(this,
83                                       left, top, width, height,
84                                       config, buffer, rowBytes,
85                                       pixelOpsFlags);
86 }
87
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) {
94         return;
95     }
96     context->writeTexturePixels(this,
97                                 left, top, width, height,
98                                 config, buffer, rowBytes,
99                                 pixelOpsFlags);
100 }
101
102 void GrTexture::onRelease() {
103     SkASSERT(!this->isSetFlag((GrTextureFlags) kReturnToCache_FlagBit));
104     INHERITED::onRelease();
105 }
106
107 void GrTexture::onAbandon() {
108     if (NULL != fRenderTarget.get()) {
109         fRenderTarget->abandon();
110     }
111     INHERITED::onAbandon();
112 }
113
114 void GrTexture::validateDesc() const {
115     if (NULL != this->asRenderTarget()) {
116         // This texture has a render target
117         SkASSERT(0 != (fDesc.fFlags & kRenderTarget_GrTextureFlagBit));
118
119         if (NULL != this->asRenderTarget()->getStencilBuffer()) {
120             SkASSERT(0 != (fDesc.fFlags & kNoStencil_GrTextureFlagBit));
121         } else {
122             SkASSERT(0 == (fDesc.fFlags & kNoStencil_GrTextureFlagBit));
123         }
124
125         SkASSERT(fDesc.fSampleCnt == this->asRenderTarget()->numSamples());
126     } else {
127         SkASSERT(0 == (fDesc.fFlags & kRenderTarget_GrTextureFlagBit));
128         SkASSERT(0 == (fDesc.fFlags & kNoStencil_GrTextureFlagBit));
129         SkASSERT(0 == fDesc.fSampleCnt);
130     }
131 }
132
133 // These flags need to fit in a GrResourceKey::ResourceFlags so they can be folded into the texture
134 // key
135 enum TextureFlags {
136     /**
137      * The kStretchToPOT bit is set when the texture is NPOT and is being repeated but the
138      * hardware doesn't support that feature.
139      */
140     kStretchToPOT_TextureFlag = 0x1,
141     /**
142      * The kBilerp bit can only be set when the kStretchToPOT flag is set and indicates whether the
143      * stretched texture should be bilerped.
144      */
145      kBilerp_TextureFlag       = 0x2,
146 };
147
148 namespace {
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:
159                     break;
160                 case GrTextureParams::kBilerp_FilterMode:
161                 case GrTextureParams::kMipMap_FilterMode:
162                     flags |= kBilerp_TextureFlag;
163                     break;
164             }
165         }
166     }
167     return flags;
168 }
169
170 GrResourceKey::ResourceType texture_resource_type() {
171     static const GrResourceKey::ResourceType gType = GrResourceKey::GenerateResourceType();
172     return gType;
173 }
174
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;
183     } else {
184         return desc.fOrigin;
185     }
186 }
187 }
188
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);
195 }
196
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
200     // descriptor.
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);
211
212     GrCacheID cacheID(GrResourceKey::ScratchDomain(), idKey);
213     return GrResourceKey(cacheID, texture_resource_type(), 0);
214 }
215
216 bool GrTexture::NeedsResizing(const GrResourceKey& key) {
217     return SkToBool(key.getResourceFlags() & kStretchToPOT_TextureFlag);
218 }
219
220 bool GrTexture::NeedsBilerp(const GrResourceKey& key) {
221     return SkToBool(key.getResourceFlags() & kBilerp_TextureFlag);
222 }