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#include "GrContext.h"
10#include "GrDrawTargetCaps.h"
11#include "GrGpu.h"
12#include "GrResourceKey.h"
13#include "GrRenderTarget.h"
14#include "GrRenderTargetPriv.h"
15#include "GrTexture.h"
16#include "GrTexturePriv.h"
17
18void GrTexture::dirtyMipMaps(bool mipMapsDirty) {
19    if (mipMapsDirty) {
20        if (kValid_MipMapsStatus == fMipMapsStatus) {
21           fMipMapsStatus = kAllocated_MipMapsStatus;
22        }
23    } else {
24        const bool sizeChanged = kNotAllocated_MipMapsStatus == fMipMapsStatus;
25        fMipMapsStatus = kValid_MipMapsStatus;
26        if (sizeChanged) {
27            // This must not be called until after changing fMipMapsStatus.
28            this->didChangeGpuMemorySize();
29        }
30    }
31}
32
33size_t GrTexture::onGpuMemorySize() const {
34    size_t textureSize;
35
36    if (GrPixelConfigIsCompressed(fDesc.fConfig)) {
37        textureSize = GrCompressedFormatDataSize(fDesc.fConfig, fDesc.fWidth, fDesc.fHeight);
38    } else {
39        textureSize = (size_t) fDesc.fWidth * fDesc.fHeight * GrBytesPerPixel(fDesc.fConfig);
40    }
41
42    if (this->texturePriv().hasMipMaps()) {
43        // We don't have to worry about the mipmaps being a different size than
44        // we'd expect because we never change fDesc.fWidth/fHeight.
45        textureSize *= 2;
46    }
47    return textureSize;
48}
49
50void GrTexture::validateDesc() const {
51    if (this->asRenderTarget()) {
52        // This texture has a render target
53        SkASSERT(0 != (fDesc.fFlags & kRenderTarget_GrSurfaceFlag));
54        SkASSERT(fDesc.fSampleCnt == this->asRenderTarget()->numSamples());
55    } else {
56        SkASSERT(0 == (fDesc.fFlags & kRenderTarget_GrSurfaceFlag));
57        SkASSERT(0 == fDesc.fSampleCnt);
58    }
59}
60
61//////////////////////////////////////////////////////////////////////////////
62
63namespace {
64
65// FIXME:  This should be refactored with the code in gl/GrGLGpu.cpp.
66GrSurfaceOrigin resolve_origin(const GrSurfaceDesc& desc) {
67    // By default, GrRenderTargets are GL's normal orientation so that they
68    // can be drawn to by the outside world without the client having
69    // to render upside down.
70    bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrSurfaceFlag);
71    if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
72        return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
73    } else {
74        return desc.fOrigin;
75    }
76}
77}
78
79//////////////////////////////////////////////////////////////////////////////
80GrTexture::GrTexture(GrGpu* gpu, LifeCycle lifeCycle, const GrSurfaceDesc& desc)
81    : INHERITED(gpu, lifeCycle, desc)
82    , fMipMapsStatus(kNotAllocated_MipMapsStatus) {
83
84    if (kWrapped_LifeCycle != lifeCycle && !GrPixelConfigIsCompressed(desc.fConfig)) {
85        GrScratchKey key;
86        GrTexturePriv::ComputeScratchKey(desc, &key);
87        this->setScratchKey(key);
88    }
89    // only make sense if alloc size is pow2
90    fShiftFixedX = 31 - SkCLZ(fDesc.fWidth);
91    fShiftFixedY = 31 - SkCLZ(fDesc.fHeight);
92}
93
94void GrTexturePriv::ComputeScratchKey(const GrSurfaceDesc& desc, GrScratchKey* key) {
95    static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType();
96
97    GrScratchKey::Builder builder(key, kType, 2);
98
99    GrSurfaceOrigin origin = resolve_origin(desc);
100    uint32_t flags = desc.fFlags & ~kCheckAllocation_GrSurfaceFlag;
101
102    SkASSERT(desc.fWidth <= SK_MaxU16);
103    SkASSERT(desc.fHeight <= SK_MaxU16);
104    SkASSERT(static_cast<int>(desc.fConfig) < (1 << 6));
105    SkASSERT(desc.fSampleCnt < (1 << 8));
106    SkASSERT(flags < (1 << 10));
107    SkASSERT(static_cast<int>(origin) < (1 << 8));
108
109    builder[0] = desc.fWidth | (desc.fHeight << 16);
110    builder[1] = desc.fConfig | (desc.fSampleCnt << 6) | (flags << 14) | (origin << 24);
111}
112