blob: 2373610d3edfdc3b0936b939ea018e0648296197 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkMath.h"
9#include "include/core/SkTypes.h"
10#include "include/gpu/GrContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/gpu/GrTexture.h"
12#include "include/gpu/GrTypes.h"
13#include "include/private/GrResourceKey.h"
14#include "src/core/SkMipMap.h"
15#include "src/gpu/GrCaps.h"
16#include "src/gpu/GrContextPriv.h"
17#include "src/gpu/GrGpu.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040018#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrSurfacePriv.h"
20#include "src/gpu/GrTexturePriv.h"
bsalomon@google.com8295dc12011-05-02 12:53:34 +000021
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040022void GrTexture::markMipMapsDirty() {
23 if (GrMipMapsStatus::kValid == fMipMapsStatus) {
24 fMipMapsStatus = GrMipMapsStatus::kDirty;
25 }
26}
27
28void GrTexture::markMipMapsClean() {
Greg Daniel09c94002018-06-08 22:11:51 +000029 SkASSERT(GrMipMapsStatus::kNotAllocated != fMipMapsStatus);
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040030 fMipMapsStatus = GrMipMapsStatus::kValid;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000031}
32
Robert Phillips29e52f12016-11-03 10:19:14 -040033size_t GrTexture::onGpuMemorySize() const {
Greg Daniel7fd7a8a2019-10-10 16:10:31 -040034 const GrCaps& caps = *this->getGpu()->caps();
Brian Salomon9f2b86c2019-10-22 10:37:46 -040035 return GrSurface::ComputeSize(caps, this->backendFormat(), this->dimensions(), 1,
36 this->texturePriv().mipMapped());
Robert Phillips29e52f12016-11-03 10:19:14 -040037}
38
Brian Salomond34edf32017-05-19 15:45:48 -040039/////////////////////////////////////////////////////////////////////////////
Brian Salomon9f2b86c2019-10-22 10:37:46 -040040GrTexture::GrTexture(GrGpu* gpu,
41 const SkISize& dimensions,
42 GrPixelConfig config,
43 GrProtected isProtected,
44 GrTextureType textureType,
45 GrMipMapsStatus mipMapsStatus)
46 : INHERITED(gpu, dimensions, config, isProtected)
Brian Salomone8a766b2019-07-19 14:24:36 -040047 , fTextureType(textureType)
48 , fMipMapsStatus(mipMapsStatus) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040049 if (GrMipMapsStatus::kNotAllocated == fMipMapsStatus) {
cblume55f2d2d2016-02-26 13:20:48 -080050 fMaxMipMapLevel = 0;
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040051 } else {
52 fMaxMipMapLevel = SkMipMap::ComputeLevelCount(this->width(), this->height());
cblume55f2d2d2016-02-26 13:20:48 -080053 }
bsalomon744998e2014-08-28 09:54:34 -070054}
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000055
Brian Salomon35ba6142019-01-24 13:08:59 -050056bool GrTexture::StealBackendTexture(sk_sp<GrTexture> texture,
Eric Karl914a36b2017-10-12 12:44:50 -070057 GrBackendTexture* backendTexture,
58 SkImage::BackendTextureReleaseProc* releaseProc) {
Robert Phillipsaee18c92019-09-06 11:48:27 -040059 if (!texture->unique()) {
Eric Karl914a36b2017-10-12 12:44:50 -070060 return false;
61 }
62
63 if (!texture->onStealBackendTexture(backendTexture, releaseProc)) {
64 return false;
65 }
Brian Salomon35ba6142019-01-24 13:08:59 -050066#ifdef SK_DEBUG
Robert Phillips9da87e02019-02-04 13:26:26 -050067 GrResourceCache* cache = texture->getContext()->priv().getResourceCache();
Brian Salomon35ba6142019-01-24 13:08:59 -050068 int preCount = cache->getResourceCount();
69#endif
70 // Ensure that the texture will be released by the cache when we drop the last ref.
71 // A texture that has no refs and no keys should be immediately removed.
72 if (texture->getUniqueKey().isValid()) {
73 texture->resourcePriv().removeUniqueKey();
74 }
75 if (texture->resourcePriv().getScratchKey().isValid()) {
76 texture->resourcePriv().removeScratchKey();
77 }
78#ifdef SK_DEBUG
79 texture.reset();
80 int postCount = cache->getResourceCount();
81 SkASSERT(postCount < preCount);
82#endif
Eric Karl914a36b2017-10-12 12:44:50 -070083 return true;
84}
85
kkinnunen2e6055b2016-04-22 01:48:29 -070086void GrTexture::computeScratchKey(GrScratchKey* key) const {
Brian Salomon1c53a9f2019-08-12 14:10:12 -040087 if (!this->getGpu()->caps()->isFormatCompressed(this->backendFormat())) {
Jim Van Verth1676cb92019-01-15 13:24:45 -050088 int sampleCount = 1;
Brian Salomonf2c2ba92019-07-17 09:59:59 -040089 GrRenderable renderable = GrRenderable::kNo;
90 if (const auto* rt = this->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -060091 sampleCount = rt->numSamples();
Brian Salomonf2c2ba92019-07-17 09:59:59 -040092 renderable = GrRenderable::kYes;
Jim Van Verth1676cb92019-01-15 13:24:45 -050093 }
Brian Salomon14cb4132019-09-16 13:14:47 -040094 auto isProtected = this->isProtected() ? GrProtected::kYes : GrProtected::kNo;
Brian Salomon9f2b86c2019-10-22 10:37:46 -040095 GrTexturePriv::ComputeScratchKey(this->config(), this->dimensions(), renderable,
Brian Salomon14cb4132019-09-16 13:14:47 -040096 sampleCount, this->texturePriv().mipMapped(), isProtected,
97 key);
kkinnunen2e6055b2016-04-22 01:48:29 -070098 }
99}
100
Brian Salomon14cb4132019-09-16 13:14:47 -0400101void GrTexturePriv::ComputeScratchKey(GrPixelConfig config,
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400102 SkISize dimensions,
Brian Salomon14cb4132019-09-16 13:14:47 -0400103 GrRenderable renderable,
104 int sampleCnt,
105 GrMipMapped mipMapped,
106 GrProtected isProtected,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400107 GrScratchKey* key) {
bsalomon7775c852014-12-30 12:50:52 -0800108 static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType();
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400109 SkASSERT(!dimensions.isEmpty());
Brian Salomonbdecacf2018-02-02 20:32:49 -0500110 SkASSERT(sampleCnt > 0);
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400111 SkASSERT(1 == sampleCnt || renderable == GrRenderable::kYes);
bsalomon7775c852014-12-30 12:50:52 -0800112
cblume55f2d2d2016-02-26 13:20:48 -0800113 // make sure desc.fConfig fits in 5 bits
114 SkASSERT(sk_float_log2(kLast_GrPixelConfig) <= 5);
Brian Salomon14cb4132019-09-16 13:14:47 -0400115 SkASSERT(static_cast<uint32_t>(config) < (1 << 5));
116 SkASSERT(static_cast<uint32_t>(mipMapped) <= 1);
117 SkASSERT(static_cast<uint32_t>(isProtected) <= 1);
118 SkASSERT(static_cast<uint32_t>(renderable) <= 1);
119 SkASSERT(static_cast<uint32_t>(sampleCnt) < (1 << (32 - 8)));
bsalomon7775c852014-12-30 12:50:52 -0800120
joshualittb90de312015-10-01 11:54:34 -0700121 GrScratchKey::Builder builder(key, kType, 3);
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400122 builder[0] = dimensions.width();
123 builder[1] = dimensions.height();
Brian Salomon14cb4132019-09-16 13:14:47 -0400124 builder[2] = (static_cast<uint32_t>(config) << 0)
125 | (static_cast<uint32_t>(mipMapped) << 5)
126 | (static_cast<uint32_t>(isProtected) << 6)
127 | (static_cast<uint32_t>(renderable) << 7)
128 | (static_cast<uint32_t>(sampleCnt) << 8);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000129}