Line data Source code
1 : /*
2 : * Copyright 2011 Google Inc.
3 : *
4 : * Use of this source code is governed by a BSD-style license that can be
5 : * found in the LICENSE file.
6 : */
7 :
8 : #include "GrContext.h"
9 : #include "GrCaps.h"
10 : #include "GrGpu.h"
11 : #include "GrResourceKey.h"
12 : #include "GrRenderTarget.h"
13 : #include "GrRenderTargetPriv.h"
14 : #include "GrTexture.h"
15 : #include "GrTexturePriv.h"
16 : #include "GrTypes.h"
17 : #include "SkMath.h"
18 : #include "SkMipMap.h"
19 : #include "SkTypes.h"
20 :
21 0 : void GrTexture::dirtyMipMaps(bool mipMapsDirty) {
22 0 : if (mipMapsDirty) {
23 0 : if (kValid_MipMapsStatus == fMipMapsStatus) {
24 0 : fMipMapsStatus = kAllocated_MipMapsStatus;
25 : }
26 : } else {
27 0 : const bool sizeChanged = kNotAllocated_MipMapsStatus == fMipMapsStatus;
28 0 : fMipMapsStatus = kValid_MipMapsStatus;
29 0 : if (sizeChanged) {
30 : // This must not be called until after changing fMipMapsStatus.
31 0 : this->didChangeGpuMemorySize();
32 : // TODO(http://skbug.com/4548) - The desc and scratch key should be
33 : // updated to reflect the newly-allocated mipmaps.
34 : }
35 : }
36 0 : }
37 :
38 0 : size_t GrTexture::onGpuMemorySize() const {
39 0 : return GrSurface::ComputeSize(fDesc, 1, this->texturePriv().hasMipMaps());
40 : }
41 :
42 0 : void GrTexture::validateDesc() const {
43 0 : if (this->asRenderTarget()) {
44 : // This texture has a render target
45 0 : SkASSERT(0 != (fDesc.fFlags & kRenderTarget_GrSurfaceFlag));
46 0 : SkASSERT(fDesc.fSampleCnt == this->asRenderTarget()->numColorSamples());
47 : } else {
48 0 : SkASSERT(0 == (fDesc.fFlags & kRenderTarget_GrSurfaceFlag));
49 0 : SkASSERT(0 == fDesc.fSampleCnt);
50 : }
51 0 : }
52 :
53 : //////////////////////////////////////////////////////////////////////////////
54 :
55 : namespace {
56 :
57 : // FIXME: This should be refactored with the code in gl/GrGLGpu.cpp.
58 0 : GrSurfaceOrigin resolve_origin(const GrSurfaceDesc& desc) {
59 : // By default, GrRenderTargets are GL's normal orientation so that they
60 : // can be drawn to by the outside world without the client having
61 : // to render upside down.
62 0 : bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrSurfaceFlag);
63 0 : if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
64 0 : return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
65 : } else {
66 0 : return desc.fOrigin;
67 : }
68 : }
69 : }
70 :
71 : //////////////////////////////////////////////////////////////////////////////
72 0 : GrTexture::GrTexture(GrGpu* gpu, const GrSurfaceDesc& desc, GrSLType samplerType,
73 0 : GrSamplerParams::FilterMode highestFilterMode, bool wasMipMapDataProvided)
74 : : INHERITED(gpu, desc)
75 : , fSamplerType(samplerType)
76 : , fHighestFilterMode(highestFilterMode)
77 : // Mip color mode is explicitly set after creation via GrTexturePriv
78 0 : , fMipColorMode(SkDestinationSurfaceColorMode::kLegacy) {
79 0 : if (wasMipMapDataProvided) {
80 0 : fMipMapsStatus = kValid_MipMapsStatus;
81 0 : fMaxMipMapLevel = SkMipMap::ComputeLevelCount(fDesc.fWidth, fDesc.fHeight);
82 : } else {
83 0 : fMipMapsStatus = kNotAllocated_MipMapsStatus;
84 0 : fMaxMipMapLevel = 0;
85 : }
86 0 : }
87 :
88 0 : void GrTexture::computeScratchKey(GrScratchKey* key) const {
89 0 : if (!GrPixelConfigIsCompressed(fDesc.fConfig)) {
90 0 : GrTexturePriv::ComputeScratchKey(fDesc, key);
91 : }
92 0 : }
93 :
94 0 : void GrTexturePriv::ComputeScratchKey(const GrSurfaceDesc& desc, GrScratchKey* key) {
95 0 : static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType();
96 :
97 0 : GrSurfaceOrigin origin = resolve_origin(desc);
98 0 : uint32_t flags = desc.fFlags & ~kCheckAllocation_GrSurfaceFlag;
99 :
100 : // make sure desc.fConfig fits in 5 bits
101 : SkASSERT(sk_float_log2(kLast_GrPixelConfig) <= 5);
102 0 : SkASSERT(static_cast<int>(desc.fConfig) < (1 << 5));
103 0 : SkASSERT(desc.fSampleCnt < (1 << 8));
104 0 : SkASSERT(flags < (1 << 10));
105 0 : SkASSERT(static_cast<int>(origin) < (1 << 8));
106 :
107 0 : GrScratchKey::Builder builder(key, kType, 3);
108 0 : builder[0] = desc.fWidth;
109 0 : builder[1] = desc.fHeight;
110 0 : builder[2] = desc.fConfig | (desc.fIsMipMapped << 5) | (desc.fSampleCnt << 6) | (flags << 14)
111 0 : | (origin << 24);
112 0 : }
|