blob: fa8d93bb6652b0da50569984e5d54e6974f95bc5 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/gpu/GrTypes.h"
11#include "include/private/GrResourceKey.h"
Mike Reed13711eb2020-07-14 17:16:32 -040012#include "src/core/SkMipmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrCaps.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrGpu.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040015#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrSurfacePriv.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000017#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrTexturePriv.h"
bsalomon@google.com8295dc12011-05-02 12:53:34 +000019
Robert Phillips4e105e22020-07-16 09:18:50 -040020#ifdef SK_DEBUG
21#include "include/gpu/GrDirectContext.h"
22#include "src/gpu/GrContextPriv.h"
23#endif
24
Brian Salomon8c82a872020-07-21 12:09:58 -040025void GrTexture::markMipmapsDirty() {
26 if (GrMipmapStatus::kValid == fMipmapStatus) {
27 fMipmapStatus = GrMipmapStatus::kDirty;
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040028 }
29}
30
Brian Salomon8c82a872020-07-21 12:09:58 -040031void GrTexture::markMipmapsClean() {
32 SkASSERT(GrMipmapStatus::kNotAllocated != fMipmapStatus);
33 fMipmapStatus = GrMipmapStatus::kValid;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000034}
35
Robert Phillips29e52f12016-11-03 10:19:14 -040036size_t GrTexture::onGpuMemorySize() const {
Greg Daniel7fd7a8a2019-10-10 16:10:31 -040037 const GrCaps& caps = *this->getGpu()->caps();
Brian Salomon9f2b86c2019-10-22 10:37:46 -040038 return GrSurface::ComputeSize(caps, this->backendFormat(), this->dimensions(), 1,
Brian Salomon8c82a872020-07-21 12:09:58 -040039 this->texturePriv().mipmapped());
Robert Phillips29e52f12016-11-03 10:19:14 -040040}
41
Brian Salomond34edf32017-05-19 15:45:48 -040042/////////////////////////////////////////////////////////////////////////////
Brian Salomon9f2b86c2019-10-22 10:37:46 -040043GrTexture::GrTexture(GrGpu* gpu,
44 const SkISize& dimensions,
Brian Salomon9f2b86c2019-10-22 10:37:46 -040045 GrProtected isProtected,
46 GrTextureType textureType,
Brian Salomona6db5102020-07-21 09:56:23 -040047 GrMipmapStatus mipmapStatus)
Greg Danield51fa2f2020-01-22 16:53:38 -050048 : INHERITED(gpu, dimensions, isProtected)
Brian Salomone8a766b2019-07-19 14:24:36 -040049 , fTextureType(textureType)
Brian Salomon8c82a872020-07-21 12:09:58 -040050 , fMipmapStatus(mipmapStatus) {
51 if (fMipmapStatus == GrMipmapStatus::kNotAllocated) {
52 fMaxMipmapLevel = 0;
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040053 } else {
Brian Salomon8c82a872020-07-21 12:09:58 -040054 fMaxMipmapLevel = SkMipmap::ComputeLevelCount(this->width(), this->height());
cblume55f2d2d2016-02-26 13:20:48 -080055 }
bsalomon744998e2014-08-28 09:54:34 -070056}
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000057
Brian Salomon35ba6142019-01-24 13:08:59 -050058bool GrTexture::StealBackendTexture(sk_sp<GrTexture> texture,
Eric Karl914a36b2017-10-12 12:44:50 -070059 GrBackendTexture* backendTexture,
60 SkImage::BackendTextureReleaseProc* releaseProc) {
Robert Phillipsaee18c92019-09-06 11:48:27 -040061 if (!texture->unique()) {
Eric Karl914a36b2017-10-12 12:44:50 -070062 return false;
63 }
64
65 if (!texture->onStealBackendTexture(backendTexture, releaseProc)) {
66 return false;
67 }
Brian Salomon35ba6142019-01-24 13:08:59 -050068#ifdef SK_DEBUG
Robert Phillips9da87e02019-02-04 13:26:26 -050069 GrResourceCache* cache = texture->getContext()->priv().getResourceCache();
Brian Salomon35ba6142019-01-24 13:08:59 -050070 int preCount = cache->getResourceCount();
71#endif
72 // Ensure that the texture will be released by the cache when we drop the last ref.
73 // A texture that has no refs and no keys should be immediately removed.
74 if (texture->getUniqueKey().isValid()) {
75 texture->resourcePriv().removeUniqueKey();
76 }
77 if (texture->resourcePriv().getScratchKey().isValid()) {
78 texture->resourcePriv().removeScratchKey();
79 }
80#ifdef SK_DEBUG
81 texture.reset();
82 int postCount = cache->getResourceCount();
83 SkASSERT(postCount < preCount);
84#endif
Eric Karl914a36b2017-10-12 12:44:50 -070085 return true;
86}
87
kkinnunen2e6055b2016-04-22 01:48:29 -070088void GrTexture::computeScratchKey(GrScratchKey* key) const {
Brian Salomon1c53a9f2019-08-12 14:10:12 -040089 if (!this->getGpu()->caps()->isFormatCompressed(this->backendFormat())) {
Jim Van Verth1676cb92019-01-15 13:24:45 -050090 int sampleCount = 1;
Brian Salomonf2c2ba92019-07-17 09:59:59 -040091 GrRenderable renderable = GrRenderable::kNo;
92 if (const auto* rt = this->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -060093 sampleCount = rt->numSamples();
Brian Salomonf2c2ba92019-07-17 09:59:59 -040094 renderable = GrRenderable::kYes;
Jim Van Verth1676cb92019-01-15 13:24:45 -050095 }
Brian Salomon14cb4132019-09-16 13:14:47 -040096 auto isProtected = this->isProtected() ? GrProtected::kYes : GrProtected::kNo;
Greg Danield51fa2f2020-01-22 16:53:38 -050097 GrTexturePriv::ComputeScratchKey(*this->getGpu()->caps(), this->backendFormat(),
98 this->dimensions(), renderable, sampleCount,
Brian Salomon8c82a872020-07-21 12:09:58 -040099 this->texturePriv().mipmapped(), isProtected, key);
kkinnunen2e6055b2016-04-22 01:48:29 -0700100 }
101}
102
Greg Danield51fa2f2020-01-22 16:53:38 -0500103void GrTexturePriv::ComputeScratchKey(const GrCaps& caps,
104 const GrBackendFormat& format,
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400105 SkISize dimensions,
Brian Salomon14cb4132019-09-16 13:14:47 -0400106 GrRenderable renderable,
107 int sampleCnt,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400108 GrMipmapped mipMapped,
Brian Salomon14cb4132019-09-16 13:14:47 -0400109 GrProtected isProtected,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400110 GrScratchKey* key) {
bsalomon7775c852014-12-30 12:50:52 -0800111 static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType();
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400112 SkASSERT(!dimensions.isEmpty());
Brian Salomonbdecacf2018-02-02 20:32:49 -0500113 SkASSERT(sampleCnt > 0);
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400114 SkASSERT(1 == sampleCnt || renderable == GrRenderable::kYes);
bsalomon7775c852014-12-30 12:50:52 -0800115
Brian Salomon14cb4132019-09-16 13:14:47 -0400116 SkASSERT(static_cast<uint32_t>(mipMapped) <= 1);
117 SkASSERT(static_cast<uint32_t>(isProtected) <= 1);
118 SkASSERT(static_cast<uint32_t>(renderable) <= 1);
Greg Danield51fa2f2020-01-22 16:53:38 -0500119 SkASSERT(static_cast<uint32_t>(sampleCnt) < (1 << (32 - 3)));
bsalomon7775c852014-12-30 12:50:52 -0800120
Greg Danield51fa2f2020-01-22 16:53:38 -0500121 uint64_t formatKey = caps.computeFormatKey(format);
122
123 GrScratchKey::Builder builder(key, kType, 5);
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400124 builder[0] = dimensions.width();
125 builder[1] = dimensions.height();
Greg Danield51fa2f2020-01-22 16:53:38 -0500126 builder[2] = formatKey & 0xFFFFFFFF;
127 builder[3] = (formatKey >> 32) & 0xFFFFFFFF;
128 builder[4] = (static_cast<uint32_t>(mipMapped) << 0)
129 | (static_cast<uint32_t>(isProtected) << 1)
130 | (static_cast<uint32_t>(renderable) << 2)
131 | (static_cast<uint32_t>(sampleCnt) << 3);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000132}