blob: 064cc0a22b4a1250b1a2c63a4736a60cd61db161 [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"
13#include "GrRenderTargetPriv.h"
bsalomonafbf2d62014-09-30 12:18:44 -070014#include "GrTexture.h"
15#include "GrTexturePriv.h"
cblume55f2d2d2016-02-26 13:20:48 -080016#include "GrTypes.h"
17#include "SkMath.h"
18#include "SkMipMap.h"
19#include "SkTypes.h"
bsalomon@google.com8295dc12011-05-02 12:53:34 +000020
brianosmanfe199872016-06-13 07:59:48 -070021void GrTexture::dirtyMipMaps(bool mipMapsDirty) {
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000022 if (mipMapsDirty) {
23 if (kValid_MipMapsStatus == fMipMapsStatus) {
cblume61214052016-01-26 09:10:48 -080024 fMipMapsStatus = kAllocated_MipMapsStatus;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000025 }
26 } else {
27 const bool sizeChanged = kNotAllocated_MipMapsStatus == fMipMapsStatus;
28 fMipMapsStatus = kValid_MipMapsStatus;
29 if (sizeChanged) {
30 // This must not be called until after changing fMipMapsStatus.
31 this->didChangeGpuMemorySize();
cblume55f2d2d2016-02-26 13:20:48 -080032 // TODO(http://skbug.com/4548) - The desc and scratch key should be
33 // updated to reflect the newly-allocated mipmaps.
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000034 }
35 }
36}
37
Robert Phillips29e52f12016-11-03 10:19:14 -040038size_t GrTexture::onGpuMemorySize() const {
Brian Salomonbb5711a2017-05-17 13:49:59 -040039 return GrSurface::ComputeSize(this->config(), this->width(), this->height(), 1,
40 this->texturePriv().hasMipMaps(), false);
Robert Phillips29e52f12016-11-03 10:19:14 -040041}
42
Brian Salomond34edf32017-05-19 15:45:48 -040043/////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000044
robertphillips@google.coma1e57952012-06-04 20:05:28 +000045namespace {
robertphillips@google.coma1e57952012-06-04 20:05:28 +000046
jvanverth39edf762014-12-22 11:44:19 -080047// FIXME: This should be refactored with the code in gl/GrGLGpu.cpp.
bsalomonf2703d82014-10-28 14:33:06 -070048GrSurfaceOrigin resolve_origin(const GrSurfaceDesc& desc) {
senorblanco@chromium.org709906b2013-02-07 00:35:25 +000049 // By default, GrRenderTargets are GL's normal orientation so that they
50 // can be drawn to by the outside world without the client having
51 // to render upside down.
bsalomonf2703d82014-10-28 14:33:06 -070052 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrSurfaceFlag);
senorblanco@chromium.org709906b2013-02-07 00:35:25 +000053 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
54 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
55 } else {
56 return desc.fOrigin;
57 }
58}
robertphillips@google.coma1e57952012-06-04 20:05:28 +000059}
60
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000061//////////////////////////////////////////////////////////////////////////////
kkinnunen2e6055b2016-04-22 01:48:29 -070062GrTexture::GrTexture(GrGpu* gpu, const GrSurfaceDesc& desc, GrSLType samplerType,
Brian Salomon514baff2016-11-17 15:17:07 -050063 GrSamplerParams::FilterMode highestFilterMode, bool wasMipMapDataProvided)
kkinnunen2e6055b2016-04-22 01:48:29 -070064 : INHERITED(gpu, desc)
brianosmanfe199872016-06-13 07:59:48 -070065 , fSamplerType(samplerType)
Brian Salomon739c5bf2016-11-07 09:53:44 -050066 , fHighestFilterMode(highestFilterMode)
Brian Osman7b8400d2016-11-08 17:08:54 -050067 // Mip color mode is explicitly set after creation via GrTexturePriv
68 , fMipColorMode(SkDestinationSurfaceColorMode::kLegacy) {
cblume55f2d2d2016-02-26 13:20:48 -080069 if (wasMipMapDataProvided) {
70 fMipMapsStatus = kValid_MipMapsStatus;
Brian Salomond34edf32017-05-19 15:45:48 -040071 fMaxMipMapLevel = SkMipMap::ComputeLevelCount(this->width(), this->height());
cblume55f2d2d2016-02-26 13:20:48 -080072 } else {
73 fMipMapsStatus = kNotAllocated_MipMapsStatus;
74 fMaxMipMapLevel = 0;
75 }
bsalomon744998e2014-08-28 09:54:34 -070076}
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000077
kkinnunen2e6055b2016-04-22 01:48:29 -070078void GrTexture::computeScratchKey(GrScratchKey* key) const {
Robert Phillipsee263632017-05-22 13:59:44 -040079 const GrRenderTarget* rt = this->asRenderTarget();
80 int sampleCount = 0;
81 if (rt) {
82 sampleCount = rt->numStencilSamples();
kkinnunen2e6055b2016-04-22 01:48:29 -070083 }
Robert Phillipsee263632017-05-22 13:59:44 -040084 GrTexturePriv::ComputeScratchKey(this->config(), this->width(), this->height(),
85 this->origin(), SkToBool(rt), sampleCount,
86 this->texturePriv().hasMipMaps(), key);
kkinnunen2e6055b2016-04-22 01:48:29 -070087}
88
Brian Salomond34edf32017-05-19 15:45:48 -040089void GrTexturePriv::ComputeScratchKey(GrPixelConfig config, int width, int height,
90 GrSurfaceOrigin origin, bool isRenderTarget, int sampleCnt,
91 bool isMipMapped, GrScratchKey* key) {
bsalomon7775c852014-12-30 12:50:52 -080092 static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType();
Brian Salomond34edf32017-05-19 15:45:48 -040093 uint32_t flags = isRenderTarget;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000094
Brian Salomond34edf32017-05-19 15:45:48 -040095 SkASSERT(0 == sampleCnt || isRenderTarget);
bsalomon7775c852014-12-30 12:50:52 -080096
cblume55f2d2d2016-02-26 13:20:48 -080097 // make sure desc.fConfig fits in 5 bits
98 SkASSERT(sk_float_log2(kLast_GrPixelConfig) <= 5);
Brian Salomond34edf32017-05-19 15:45:48 -040099 SkASSERT(static_cast<int>(config) < (1 << 5));
100 SkASSERT(sampleCnt < (1 << 8));
bsalomon7775c852014-12-30 12:50:52 -0800101 SkASSERT(flags < (1 << 10));
102 SkASSERT(static_cast<int>(origin) < (1 << 8));
103
joshualittb90de312015-10-01 11:54:34 -0700104 GrScratchKey::Builder builder(key, kType, 3);
Brian Salomond34edf32017-05-19 15:45:48 -0400105 builder[0] = width;
106 builder[1] = height;
107 builder[2] = config | (isMipMapped << 5) | (sampleCnt << 6) | (flags << 14) | (origin << 24);
108}
109
110void GrTexturePriv::ComputeScratchKey(const GrSurfaceDesc& desc, GrScratchKey* key) {
111 GrSurfaceOrigin origin = resolve_origin(desc);
112 return ComputeScratchKey(desc.fConfig, desc.fWidth, desc.fHeight, origin,
113 SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag), desc.fSampleCnt,
114 desc.fIsMipMapped, key);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000115}