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