1669fdc4ed8ed461a141cb97d0afdd9ef72a82be1bsalomon@google.com
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/*
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2011 Google Inc.
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
7669fdc4ed8ed461a141cb97d0afdd9ef72a82be1bsalomon@google.com */
8669fdc4ed8ed461a141cb97d0afdd9ef72a82be1bsalomon@google.com
9ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
10669fdc4ed8ed461a141cb97d0afdd9ef72a82be1bsalomon@google.com#include "GrTexture.h"
11aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
12669fdc4ed8ed461a141cb97d0afdd9ef72a82be1bsalomon@google.com#include "GrContext.h"
13c26d94fd7dc0b00cd6d0e42d28285f4a38aff021bsalomon@google.com#include "GrDrawTargetCaps.h"
1405ef510389950e1ae8dcba40e41e001db771b12dbsalomon@google.com#include "GrGpu.h"
15aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com#include "GrRenderTarget.h"
16a1e5795279bd085496fa04c59e588f440bf30457robertphillips@google.com#include "GrResourceCache.h"
178295dc1474db279df08d816b2115e807c681fad5bsalomon@google.com
18686bcb871b8425603b9accbf72e27a9309f786d8bsalomon@google.comGrTexture::~GrTexture() {
19686bcb871b8425603b9accbf72e27a9309f786d8bsalomon@google.com    if (NULL != fRenderTarget.get()) {
20686bcb871b8425603b9accbf72e27a9309f786d8bsalomon@google.com        fRenderTarget.get()->owningTextureDestroyed();
21686bcb871b8425603b9accbf72e27a9309f786d8bsalomon@google.com    }
22686bcb871b8425603b9accbf72e27a9309f786d8bsalomon@google.com}
23686bcb871b8425603b9accbf72e27a9309f786d8bsalomon@google.com
24d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com/**
2515c0fea699b25343fe6f49668a5632866e1a0306robertphillips@google.com * This method allows us to interrupt the normal deletion process and place
2615c0fea699b25343fe6f49668a5632866e1a0306robertphillips@google.com * textures back in the texture cache when their ref count goes to zero.
2715c0fea699b25343fe6f49668a5632866e1a0306robertphillips@google.com */
2815c0fea699b25343fe6f49668a5632866e1a0306robertphillips@google.comvoid GrTexture::internal_dispose() const {
29e49157f0838c615b10da0ef2b297c742da662e34commit-bot@chromium.org    if (this->impl()->isSetFlag((GrTextureFlags) GrTextureImpl::kReturnToCache_FlagBit) &&
3015c0fea699b25343fe6f49668a5632866e1a0306robertphillips@google.com        NULL != this->INHERITED::getContext()) {
3115c0fea699b25343fe6f49668a5632866e1a0306robertphillips@google.com        GrTexture* nonConstThis = const_cast<GrTexture *>(this);
3215c0fea699b25343fe6f49668a5632866e1a0306robertphillips@google.com        this->fRefCnt = 1;      // restore ref count to initial setting
3315c0fea699b25343fe6f49668a5632866e1a0306robertphillips@google.com
34e49157f0838c615b10da0ef2b297c742da662e34commit-bot@chromium.org        nonConstThis->impl()->resetFlag((GrTextureFlags) GrTextureImpl::kReturnToCache_FlagBit);
3515c0fea699b25343fe6f49668a5632866e1a0306robertphillips@google.com        nonConstThis->INHERITED::getContext()->addExistingTextureToCache(nonConstThis);
3615c0fea699b25343fe6f49668a5632866e1a0306robertphillips@google.com
37d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        // Note: "this" texture might be freed inside addExistingTextureToCache
38f41f4d22576c95f0e3ddfc87be40fcf4ee34b408robertphillips@google.com        // if it is purged.
3915c0fea699b25343fe6f49668a5632866e1a0306robertphillips@google.com        return;
4015c0fea699b25343fe6f49668a5632866e1a0306robertphillips@google.com    }
4115c0fea699b25343fe6f49668a5632866e1a0306robertphillips@google.com
420255a5d2fe37f5b5b4e7c4cdb2ec3ab18a53deafrobertphillips@google.com    SkASSERT(0 == this->getDeferredRefCount());
4315c0fea699b25343fe6f49668a5632866e1a0306robertphillips@google.com    this->INHERITED::internal_dispose();
4415c0fea699b25343fe6f49668a5632866e1a0306robertphillips@google.com}
4515c0fea699b25343fe6f49668a5632866e1a0306robertphillips@google.com
46e49157f0838c615b10da0ef2b297c742da662e34commit-bot@chromium.orgvoid GrTextureImpl::dirtyMipMaps(bool mipMapsDirty) {
4711c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org    if (mipMapsDirty) {
4811c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        if (kValid_MipMapsStatus == fMipMapsStatus) {
4911c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org            fMipMapsStatus = kAllocated_MipMapsStatus;
5011c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        }
5111c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org    } else {
5211c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        const bool sizeChanged = kNotAllocated_MipMapsStatus == fMipMapsStatus;
5311c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        fMipMapsStatus = kValid_MipMapsStatus;
5411c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        if (sizeChanged) {
5511c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org            // This must not be called until after changing fMipMapsStatus.
5611c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org            this->didChangeGpuMemorySize();
5711c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        }
5811c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org    }
5911c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org}
6011c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org
6111c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.orgsize_t GrTexture::gpuMemorySize() const {
6211c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org    size_t textureSize =  (size_t) fDesc.fWidth *
6311c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org                                   fDesc.fHeight *
6411c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org                                   GrBytesPerPixel(fDesc.fConfig);
656e7ddaae0a077a777b8b8872ec27f8faab275536commit-bot@chromium.org
666e7ddaae0a077a777b8b8872ec27f8faab275536commit-bot@chromium.org    if (GrPixelConfigIsCompressed(fDesc.fConfig)) {
676e7ddaae0a077a777b8b8872ec27f8faab275536commit-bot@chromium.org        // Figure out the width and height corresponding to the data...
686e7ddaae0a077a777b8b8872ec27f8faab275536commit-bot@chromium.org
696e7ddaae0a077a777b8b8872ec27f8faab275536commit-bot@chromium.org        // Both of the available formats (ETC1 and LATC) have 4x4
706e7ddaae0a077a777b8b8872ec27f8faab275536commit-bot@chromium.org        // blocks that compress down to 8 bytes.
716e7ddaae0a077a777b8b8872ec27f8faab275536commit-bot@chromium.org        switch(fDesc.fConfig) {
726e7ddaae0a077a777b8b8872ec27f8faab275536commit-bot@chromium.org            case kETC1_GrPixelConfig:
736e7ddaae0a077a777b8b8872ec27f8faab275536commit-bot@chromium.org            case kLATC_GrPixelConfig:
746e7ddaae0a077a777b8b8872ec27f8faab275536commit-bot@chromium.org                SkASSERT((fDesc.fWidth & 3) == 0);
756e7ddaae0a077a777b8b8872ec27f8faab275536commit-bot@chromium.org                SkASSERT((fDesc.fHeight & 3) == 0);
766e7ddaae0a077a777b8b8872ec27f8faab275536commit-bot@chromium.org                textureSize = (fDesc.fWidth >> 2) * (fDesc.fHeight >> 2) * 8;
776e7ddaae0a077a777b8b8872ec27f8faab275536commit-bot@chromium.org                break;
786e7ddaae0a077a777b8b8872ec27f8faab275536commit-bot@chromium.org
796e7ddaae0a077a777b8b8872ec27f8faab275536commit-bot@chromium.org            default:
806e7ddaae0a077a777b8b8872ec27f8faab275536commit-bot@chromium.org                SkFAIL("Unknown compressed config");
816e7ddaae0a077a777b8b8872ec27f8faab275536commit-bot@chromium.org        }
826e7ddaae0a077a777b8b8872ec27f8faab275536commit-bot@chromium.org    }
836e7ddaae0a077a777b8b8872ec27f8faab275536commit-bot@chromium.org
84e49157f0838c615b10da0ef2b297c742da662e34commit-bot@chromium.org    if (this->impl()->hasMipMaps()) {
8511c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        // We don't have to worry about the mipmaps being a different size than
8611c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        // we'd expect because we never change fDesc.fWidth/fHeight.
8711c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org        textureSize *= 2;
8811c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org    }
8911c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org    return textureSize;
9011c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org}
9111c6b39cfa24f812ceb115589f51a60a56ef14fecommit-bot@chromium.org
92669fdc4ed8ed461a141cb97d0afdd9ef72a82be1bsalomon@google.combool GrTexture::readPixels(int left, int top, int width, int height,
936f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com                           GrPixelConfig config, void* buffer,
940342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                           size_t rowBytes, uint32_t pixelOpsFlags) {
95669fdc4ed8ed461a141cb97d0afdd9ef72a82be1bsalomon@google.com    // go through context so that all necessary flushing occurs
966f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com    GrContext* context = this->getContext();
976f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com    if (NULL == context) {
986f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com        return false;
996f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com    }
100669fdc4ed8ed461a141cb97d0afdd9ef72a82be1bsalomon@google.com    return context->readTexturePixels(this,
1010342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                                      left, top, width, height,
1020342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                                      config, buffer, rowBytes,
1030342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                                      pixelOpsFlags);
1046f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com}
1056f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com
1066f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.comvoid GrTexture::writePixels(int left, int top, int width, int height,
1076f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com                            GrPixelConfig config, const void* buffer,
1080342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                            size_t rowBytes, uint32_t pixelOpsFlags) {
1096f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com    // go through context so that all necessary flushing occurs
1106f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com    GrContext* context = this->getContext();
1116f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com    if (NULL == context) {
1126f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com        return;
1136f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com    }
1146f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com    context->writeTexturePixels(this,
1150342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                                left, top, width, height,
1160342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                                config, buffer, rowBytes,
1170342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                                pixelOpsFlags);
118669fdc4ed8ed461a141cb97d0afdd9ef72a82be1bsalomon@google.com}
119cee661af926cc977addc6e039b7022975a448acebsalomon@google.com
12015c0fea699b25343fe6f49668a5632866e1a0306robertphillips@google.comvoid GrTexture::onRelease() {
121e49157f0838c615b10da0ef2b297c742da662e34commit-bot@chromium.org    SkASSERT(!this->impl()->isSetFlag((GrTextureFlags) GrTextureImpl::kReturnToCache_FlagBit));
122d364554bcfd391c3b6111af8bff963a35ab87ba7robertphillips@google.com    INHERITED::onRelease();
12315c0fea699b25343fe6f49668a5632866e1a0306robertphillips@google.com}
12415c0fea699b25343fe6f49668a5632866e1a0306robertphillips@google.com
125aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.comvoid GrTexture::onAbandon() {
126686bcb871b8425603b9accbf72e27a9309f786d8bsalomon@google.com    if (NULL != fRenderTarget.get()) {
127aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com        fRenderTarget->abandon();
128aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    }
129d364554bcfd391c3b6111af8bff963a35ab87ba7robertphillips@google.com    INHERITED::onAbandon();
130aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com}
131aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
13232716283420df90644c8b8114308f7967aa91d9frobertphillips@google.comvoid GrTexture::validateDesc() const {
13332716283420df90644c8b8114308f7967aa91d9frobertphillips@google.com    if (NULL != this->asRenderTarget()) {
13432716283420df90644c8b8114308f7967aa91d9frobertphillips@google.com        // This texture has a render target
135f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org        SkASSERT(0 != (fDesc.fFlags & kRenderTarget_GrTextureFlagBit));
13632716283420df90644c8b8114308f7967aa91d9frobertphillips@google.com
13732716283420df90644c8b8114308f7967aa91d9frobertphillips@google.com        if (NULL != this->asRenderTarget()->getStencilBuffer()) {
138f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org            SkASSERT(0 != (fDesc.fFlags & kNoStencil_GrTextureFlagBit));
13932716283420df90644c8b8114308f7967aa91d9frobertphillips@google.com        } else {
140f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org            SkASSERT(0 == (fDesc.fFlags & kNoStencil_GrTextureFlagBit));
14132716283420df90644c8b8114308f7967aa91d9frobertphillips@google.com        }
14232716283420df90644c8b8114308f7967aa91d9frobertphillips@google.com
143f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org        SkASSERT(fDesc.fSampleCnt == this->asRenderTarget()->numSamples());
14432716283420df90644c8b8114308f7967aa91d9frobertphillips@google.com    } else {
145f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org        SkASSERT(0 == (fDesc.fFlags & kRenderTarget_GrTextureFlagBit));
146f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org        SkASSERT(0 == (fDesc.fFlags & kNoStencil_GrTextureFlagBit));
147f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org        SkASSERT(0 == fDesc.fSampleCnt);
14832716283420df90644c8b8114308f7967aa91d9frobertphillips@google.com    }
14932716283420df90644c8b8114308f7967aa91d9frobertphillips@google.com}
150a1e5795279bd085496fa04c59e588f440bf30457robertphillips@google.com
151e49157f0838c615b10da0ef2b297c742da662e34commit-bot@chromium.org//////////////////////////////////////////////////////////////////////////////
152e49157f0838c615b10da0ef2b297c742da662e34commit-bot@chromium.org
1530797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com// These flags need to fit in a GrResourceKey::ResourceFlags so they can be folded into the texture
15446a8600405e678718271f62c5994119b8d3241e9robertphillips@google.com// key
1550797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.comenum TextureFlags {
1560797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com    /**
1570797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com     * The kStretchToPOT bit is set when the texture is NPOT and is being repeated but the
1580797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com     * hardware doesn't support that feature.
159a1e5795279bd085496fa04c59e588f440bf30457robertphillips@google.com     */
1600797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com    kStretchToPOT_TextureFlag = 0x1,
1610797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com    /**
162b86add1ad37776818e1f730359ec587c9fdbff5fhumper@google.com     * The kBilerp bit can only be set when the kStretchToPOT flag is set and indicates whether the
163b86add1ad37776818e1f730359ec587c9fdbff5fhumper@google.com     * stretched texture should be bilerped.
164a1e5795279bd085496fa04c59e588f440bf30457robertphillips@google.com     */
165b86add1ad37776818e1f730359ec587c9fdbff5fhumper@google.com     kBilerp_TextureFlag       = 0x2,
166a1e5795279bd085496fa04c59e588f440bf30457robertphillips@google.com};
167a1e5795279bd085496fa04c59e588f440bf30457robertphillips@google.com
168a1e5795279bd085496fa04c59e588f440bf30457robertphillips@google.comnamespace {
1690797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.comGrResourceKey::ResourceFlags get_texture_flags(const GrGpu* gpu,
1700797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com                                               const GrTextureParams* params,
1710797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com                                               const GrTextureDesc& desc) {
1720797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com    GrResourceKey::ResourceFlags flags = 0;
1730797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com    bool tiled = NULL != params && params->isTiled();
174bcce8926524827775539874346dd424a9510dbc9bsalomon@google.com    if (tiled && !gpu->caps()->npotTextureTileSupport()) {
175f9dae780c2c46a1f08adbbe8de2faaba091254d4tfarina        if (!SkIsPow2(desc.fWidth) || !SkIsPow2(desc.fHeight)) {
1760797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com            flags |= kStretchToPOT_TextureFlag;
177b86add1ad37776818e1f730359ec587c9fdbff5fhumper@google.com            switch(params->filterMode()) {
178b86add1ad37776818e1f730359ec587c9fdbff5fhumper@google.com                case GrTextureParams::kNone_FilterMode:
179b86add1ad37776818e1f730359ec587c9fdbff5fhumper@google.com                    break;
180b86add1ad37776818e1f730359ec587c9fdbff5fhumper@google.com                case GrTextureParams::kBilerp_FilterMode:
181b86add1ad37776818e1f730359ec587c9fdbff5fhumper@google.com                case GrTextureParams::kMipMap_FilterMode:
182b86add1ad37776818e1f730359ec587c9fdbff5fhumper@google.com                    flags |= kBilerp_TextureFlag;
183b86add1ad37776818e1f730359ec587c9fdbff5fhumper@google.com                    break;
184a1e5795279bd085496fa04c59e588f440bf30457robertphillips@google.com            }
185a1e5795279bd085496fa04c59e588f440bf30457robertphillips@google.com        }
186a1e5795279bd085496fa04c59e588f440bf30457robertphillips@google.com    }
1870797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com    return flags;
1880797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com}
189a1e5795279bd085496fa04c59e588f440bf30457robertphillips@google.com
1900797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.comGrResourceKey::ResourceType texture_resource_type() {
1910797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com    static const GrResourceKey::ResourceType gType = GrResourceKey::GenerateResourceType();
1920797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com    return gType;
193a1e5795279bd085496fa04c59e588f440bf30457robertphillips@google.com}
194709906b74dc0179609e9f1455dc6e9e13675c0fasenorblanco@chromium.org
195709906b74dc0179609e9f1455dc6e9e13675c0fasenorblanco@chromium.org// FIXME:  This should be refactored with the code in gl/GrGpuGL.cpp.
196709906b74dc0179609e9f1455dc6e9e13675c0fasenorblanco@chromium.orgGrSurfaceOrigin resolve_origin(const GrTextureDesc& desc) {
197709906b74dc0179609e9f1455dc6e9e13675c0fasenorblanco@chromium.org    // By default, GrRenderTargets are GL's normal orientation so that they
198709906b74dc0179609e9f1455dc6e9e13675c0fasenorblanco@chromium.org    // can be drawn to by the outside world without the client having
199709906b74dc0179609e9f1455dc6e9e13675c0fasenorblanco@chromium.org    // to render upside down.
200709906b74dc0179609e9f1455dc6e9e13675c0fasenorblanco@chromium.org    bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
201709906b74dc0179609e9f1455dc6e9e13675c0fasenorblanco@chromium.org    if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
202709906b74dc0179609e9f1455dc6e9e13675c0fasenorblanco@chromium.org        return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
203709906b74dc0179609e9f1455dc6e9e13675c0fasenorblanco@chromium.org    } else {
204709906b74dc0179609e9f1455dc6e9e13675c0fasenorblanco@chromium.org        return desc.fOrigin;
205709906b74dc0179609e9f1455dc6e9e13675c0fasenorblanco@chromium.org    }
206709906b74dc0179609e9f1455dc6e9e13675c0fasenorblanco@chromium.org}
207a1e5795279bd085496fa04c59e588f440bf30457robertphillips@google.com}
208a1e5795279bd085496fa04c59e588f440bf30457robertphillips@google.com
209e49157f0838c615b10da0ef2b297c742da662e34commit-bot@chromium.org//////////////////////////////////////////////////////////////////////////////
210e49157f0838c615b10da0ef2b297c742da662e34commit-bot@chromium.org
211e49157f0838c615b10da0ef2b297c742da662e34commit-bot@chromium.orgGrResourceKey GrTextureImpl::ComputeKey(const GrGpu* gpu,
212b8670998a59d305cd22a3c0cbdc6e075b0a37a6ebsalomon@google.com                                    const GrTextureParams* params,
213a1e5795279bd085496fa04c59e588f440bf30457robertphillips@google.com                                    const GrTextureDesc& desc,
2140797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com                                    const GrCacheID& cacheID) {
2150797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com    GrResourceKey::ResourceFlags flags = get_texture_flags(gpu, params, desc);
2160797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com    return GrResourceKey(cacheID, texture_resource_type(), flags);
217a1e5795279bd085496fa04c59e588f440bf30457robertphillips@google.com}
218a1e5795279bd085496fa04c59e588f440bf30457robertphillips@google.com
219e49157f0838c615b10da0ef2b297c742da662e34commit-bot@chromium.orgGrResourceKey GrTextureImpl::ComputeScratchKey(const GrTextureDesc& desc) {
2200797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com    GrCacheID::Key idKey;
2210797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com    // Instead of a client-provided key of the texture contents we create a key from the
2220797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com    // descriptor.
2233cb406bb88f5aa09cf9f5a9554b4b1314cf1a2eesenorblanco@chromium.org    GR_STATIC_ASSERT(sizeof(idKey) >= 16);
224f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org    SkASSERT(desc.fHeight < (1 << 16));
225f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org    SkASSERT(desc.fWidth < (1 << 16));
2260797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com    idKey.fData32[0] = (desc.fWidth) | (desc.fHeight << 16);
2270797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com    idKey.fData32[1] = desc.fConfig | desc.fSampleCnt << 16;
2280797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com    idKey.fData32[2] = desc.fFlags;
229709906b74dc0179609e9f1455dc6e9e13675c0fasenorblanco@chromium.org    idKey.fData32[3] = resolve_origin(desc);    // Only needs 2 bits actually
2303cb406bb88f5aa09cf9f5a9554b4b1314cf1a2eesenorblanco@chromium.org    static const int kPadSize = sizeof(idKey) - 16;
2313cb406bb88f5aa09cf9f5a9554b4b1314cf1a2eesenorblanco@chromium.org    GR_STATIC_ASSERT(kPadSize >= 0);
2323cb406bb88f5aa09cf9f5a9554b4b1314cf1a2eesenorblanco@chromium.org    memset(idKey.fData8 + 16, 0, kPadSize);
2330797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com
2340797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com    GrCacheID cacheID(GrResourceKey::ScratchDomain(), idKey);
2350797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com    return GrResourceKey(cacheID, texture_resource_type(), 0);
236a1e5795279bd085496fa04c59e588f440bf30457robertphillips@google.com}
237a1e5795279bd085496fa04c59e588f440bf30457robertphillips@google.com
238e49157f0838c615b10da0ef2b297c742da662e34commit-bot@chromium.orgbool GrTextureImpl::NeedsResizing(const GrResourceKey& key) {
2390797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com    return SkToBool(key.getResourceFlags() & kStretchToPOT_TextureFlag);
240a1e5795279bd085496fa04c59e588f440bf30457robertphillips@google.com}
241a1e5795279bd085496fa04c59e588f440bf30457robertphillips@google.com
242e49157f0838c615b10da0ef2b297c742da662e34commit-bot@chromium.orgbool GrTextureImpl::NeedsBilerp(const GrResourceKey& key) {
243b86add1ad37776818e1f730359ec587c9fdbff5fhumper@google.com    return SkToBool(key.getResourceFlags() & kBilerp_TextureFlag);
244a1e5795279bd085496fa04c59e588f440bf30457robertphillips@google.com}
245