blob: 4f4e5aa2663220e8942456aa447bd72230d571f3 [file] [log] [blame]
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * 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.
bsalomon@google.com669fdc42011-04-05 17:08:27 +00006 */
7
bsalomon@google.com669fdc42011-04-05 17:08:27 +00008#include "GrContext.h"
bsalomoneb1cb5c2015-05-22 08:01:09 -07009#include "GrCaps.h"
bsalomon@google.com05ef5102011-05-02 21:14:59 +000010#include "GrGpu.h"
bsalomon7775c852014-12-30 12:50:52 -080011#include "GrResourceKey.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080012#include "GrRenderTarget.h"
bsalomonafbf2d62014-09-30 12:18:44 -070013#include "GrTexture.h"
14#include "GrTexturePriv.h"
cblume55f2d2d2016-02-26 13:20:48 -080015#include "GrTypes.h"
16#include "SkMath.h"
17#include "SkMipMap.h"
18#include "SkTypes.h"
bsalomon@google.com8295dc12011-05-02 12:53:34 +000019
brianosmanfe199872016-06-13 07:59:48 -070020void GrTexture::dirtyMipMaps(bool mipMapsDirty) {
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000021 if (mipMapsDirty) {
22 if (kValid_MipMapsStatus == fMipMapsStatus) {
cblume61214052016-01-26 09:10:48 -080023 fMipMapsStatus = kAllocated_MipMapsStatus;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000024 }
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();
cblume55f2d2d2016-02-26 13:20:48 -080031 // TODO(http://skbug.com/4548) - The desc and scratch key should be
32 // updated to reflect the newly-allocated mipmaps.
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000033 }
34 }
35}
36
Robert Phillips29e52f12016-11-03 10:19:14 -040037size_t GrTexture::onGpuMemorySize() const {
Brian Salomonbb5711a2017-05-17 13:49:59 -040038 return GrSurface::ComputeSize(this->config(), this->width(), this->height(), 1,
39 this->texturePriv().hasMipMaps(), false);
Robert Phillips29e52f12016-11-03 10:19:14 -040040}
41
Brian Salomond34edf32017-05-19 15:45:48 -040042/////////////////////////////////////////////////////////////////////////////
Robert Phillips7294b852017-08-01 13:51:44 +000043
44namespace {
45
46// FIXME: This should be refactored with the code in gl/GrGLGpu.cpp.
47GrSurfaceOrigin 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//////////////////////////////////////////////////////////////////////////////
kkinnunen2e6055b2016-04-22 01:48:29 -070061GrTexture::GrTexture(GrGpu* gpu, const GrSurfaceDesc& desc, GrSLType samplerType,
Brian Salomon514baff2016-11-17 15:17:07 -050062 GrSamplerParams::FilterMode highestFilterMode, bool wasMipMapDataProvided)
kkinnunen2e6055b2016-04-22 01:48:29 -070063 : INHERITED(gpu, desc)
brianosmanfe199872016-06-13 07:59:48 -070064 , fSamplerType(samplerType)
Brian Salomon739c5bf2016-11-07 09:53:44 -050065 , fHighestFilterMode(highestFilterMode)
Brian Osman7b8400d2016-11-08 17:08:54 -050066 // Mip color mode is explicitly set after creation via GrTexturePriv
67 , fMipColorMode(SkDestinationSurfaceColorMode::kLegacy) {
cblume55f2d2d2016-02-26 13:20:48 -080068 if (wasMipMapDataProvided) {
69 fMipMapsStatus = kValid_MipMapsStatus;
Brian Salomond34edf32017-05-19 15:45:48 -040070 fMaxMipMapLevel = SkMipMap::ComputeLevelCount(this->width(), this->height());
cblume55f2d2d2016-02-26 13:20:48 -080071 } else {
72 fMipMapsStatus = kNotAllocated_MipMapsStatus;
73 fMaxMipMapLevel = 0;
74 }
bsalomon744998e2014-08-28 09:54:34 -070075}
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000076
kkinnunen2e6055b2016-04-22 01:48:29 -070077void GrTexture::computeScratchKey(GrScratchKey* key) const {
Robert Phillips92de6312017-05-23 07:43:48 -040078 const GrRenderTarget* rt = this->asRenderTarget();
79 int sampleCount = 0;
80 if (rt) {
81 sampleCount = rt->numStencilSamples();
kkinnunen2e6055b2016-04-22 01:48:29 -070082 }
Robert Phillips92de6312017-05-23 07:43:48 -040083 GrTexturePriv::ComputeScratchKey(this->config(), this->width(), this->height(),
Robert Phillips7294b852017-08-01 13:51:44 +000084 this->origin(), SkToBool(rt), sampleCount,
Robert Phillips92de6312017-05-23 07:43:48 -040085 this->texturePriv().hasMipMaps(), key);
kkinnunen2e6055b2016-04-22 01:48:29 -070086}
87
Brian Salomond34edf32017-05-19 15:45:48 -040088void GrTexturePriv::ComputeScratchKey(GrPixelConfig config, int width, int height,
Robert Phillips7294b852017-08-01 13:51:44 +000089 GrSurfaceOrigin origin, bool isRenderTarget, int sampleCnt,
Brian Salomond34edf32017-05-19 15:45:48 -040090 bool isMipMapped, GrScratchKey* key) {
bsalomon7775c852014-12-30 12:50:52 -080091 static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType();
Brian Salomond34edf32017-05-19 15:45:48 -040092 uint32_t flags = isRenderTarget;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000093
Brian Salomond34edf32017-05-19 15:45:48 -040094 SkASSERT(0 == sampleCnt || isRenderTarget);
bsalomon7775c852014-12-30 12:50:52 -080095
cblume55f2d2d2016-02-26 13:20:48 -080096 // make sure desc.fConfig fits in 5 bits
97 SkASSERT(sk_float_log2(kLast_GrPixelConfig) <= 5);
Brian Salomond34edf32017-05-19 15:45:48 -040098 SkASSERT(static_cast<int>(config) < (1 << 5));
99 SkASSERT(sampleCnt < (1 << 8));
bsalomon7775c852014-12-30 12:50:52 -0800100 SkASSERT(flags < (1 << 10));
Robert Phillips7294b852017-08-01 13:51:44 +0000101 SkASSERT(static_cast<int>(origin) < (1 << 8));
bsalomon7775c852014-12-30 12:50:52 -0800102
joshualittb90de312015-10-01 11:54:34 -0700103 GrScratchKey::Builder builder(key, kType, 3);
Brian Salomond34edf32017-05-19 15:45:48 -0400104 builder[0] = width;
105 builder[1] = height;
Robert Phillips7294b852017-08-01 13:51:44 +0000106 builder[2] = config | (isMipMapped << 5) | (sampleCnt << 6) | (flags << 14) | (origin << 24);
Brian Salomond34edf32017-05-19 15:45:48 -0400107}
108
109void GrTexturePriv::ComputeScratchKey(const GrSurfaceDesc& desc, GrScratchKey* key) {
Robert Phillips7294b852017-08-01 13:51:44 +0000110 GrSurfaceOrigin origin = resolve_origin(desc);
111 return ComputeScratchKey(desc.fConfig, desc.fWidth, desc.fHeight, origin,
Brian Salomond34edf32017-05-19 15:45:48 -0400112 SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag), desc.fSampleCnt,
113 desc.fIsMipMapped, key);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000114}