blob: 74f295b2e1a80629145c801d38ef6ebab200c127 [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
bsalomonafbf2d62014-09-30 12:18:44 -07008#include "GrTexture.h"
Brian Salomon35ba6142019-01-24 13:08:59 -05009#include "GrCaps.h"
10#include "GrContext.h"
11#include "GrContextPriv.h"
12#include "GrGpu.h"
13#include "GrRenderTarget.h"
14#include "GrResourceKey.h"
15#include "GrSurfacePriv.h"
bsalomonafbf2d62014-09-30 12:18:44 -070016#include "GrTexturePriv.h"
cblume55f2d2d2016-02-26 13:20:48 -080017#include "GrTypes.h"
18#include "SkMath.h"
19#include "SkMipMap.h"
20#include "SkTypes.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 {
Brian Salomonbb5711a2017-05-17 13:49:59 -040034 return GrSurface::ComputeSize(this->config(), this->width(), this->height(), 1,
Greg Daniele252f082017-10-23 16:05:23 -040035 this->texturePriv().mipMapped(), false);
Robert Phillips29e52f12016-11-03 10:19:14 -040036}
37
Brian Salomond34edf32017-05-19 15:45:48 -040038/////////////////////////////////////////////////////////////////////////////
Brian Salomon60dd8c72018-07-30 10:24:13 -040039GrTexture::GrTexture(GrGpu* gpu, const GrSurfaceDesc& desc, GrTextureType textureType,
Brian Salomone632dfc2018-08-01 13:01:16 -040040 GrMipMapsStatus mipMapsStatus)
41 : INHERITED(gpu, desc), fTextureType(textureType), fMipMapsStatus(mipMapsStatus) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040042 if (GrMipMapsStatus::kNotAllocated == fMipMapsStatus) {
cblume55f2d2d2016-02-26 13:20:48 -080043 fMaxMipMapLevel = 0;
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040044 } else {
45 fMaxMipMapLevel = SkMipMap::ComputeLevelCount(this->width(), this->height());
cblume55f2d2d2016-02-26 13:20:48 -080046 }
bsalomon744998e2014-08-28 09:54:34 -070047}
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000048
Brian Salomon35ba6142019-01-24 13:08:59 -050049bool GrTexture::StealBackendTexture(sk_sp<GrTexture> texture,
Eric Karl914a36b2017-10-12 12:44:50 -070050 GrBackendTexture* backendTexture,
51 SkImage::BackendTextureReleaseProc* releaseProc) {
52 if (!texture->surfacePriv().hasUniqueRef() || texture->surfacePriv().hasPendingIO()) {
53 return false;
54 }
55
56 if (!texture->onStealBackendTexture(backendTexture, releaseProc)) {
57 return false;
58 }
Brian Salomon35ba6142019-01-24 13:08:59 -050059#ifdef SK_DEBUG
Robert Phillips9da87e02019-02-04 13:26:26 -050060 GrResourceCache* cache = texture->getContext()->priv().getResourceCache();
Brian Salomon35ba6142019-01-24 13:08:59 -050061 int preCount = cache->getResourceCount();
62#endif
63 // Ensure that the texture will be released by the cache when we drop the last ref.
64 // A texture that has no refs and no keys should be immediately removed.
65 if (texture->getUniqueKey().isValid()) {
66 texture->resourcePriv().removeUniqueKey();
67 }
68 if (texture->resourcePriv().getScratchKey().isValid()) {
69 texture->resourcePriv().removeScratchKey();
70 }
71#ifdef SK_DEBUG
72 texture.reset();
73 int postCount = cache->getResourceCount();
74 SkASSERT(postCount < preCount);
75#endif
Eric Karl914a36b2017-10-12 12:44:50 -070076 return true;
77}
78
kkinnunen2e6055b2016-04-22 01:48:29 -070079void GrTexture::computeScratchKey(GrScratchKey* key) const {
Jim Van Verth1676cb92019-01-15 13:24:45 -050080 if (!GrPixelConfigIsCompressed(this->config())) {
81 const GrRenderTarget* rt = this->asRenderTarget();
82 int sampleCount = 1;
83 if (rt) {
84 sampleCount = rt->numStencilSamples();
85 }
86 GrTexturePriv::ComputeScratchKey(this->config(), this->width(), this->height(),
87 SkToBool(rt), sampleCount,
88 this->texturePriv().mipMapped(), key);
kkinnunen2e6055b2016-04-22 01:48:29 -070089 }
90}
91
Brian Salomond34edf32017-05-19 15:45:48 -040092void GrTexturePriv::ComputeScratchKey(GrPixelConfig config, int width, int height,
Robert Phillipsb0e93a22017-08-29 08:26:54 -040093 bool isRenderTarget, int sampleCnt,
Greg Daniele252f082017-10-23 16:05:23 -040094 GrMipMapped mipMapped, GrScratchKey* key) {
bsalomon7775c852014-12-30 12:50:52 -080095 static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType();
Brian Salomond34edf32017-05-19 15:45:48 -040096 uint32_t flags = isRenderTarget;
Brian Salomonbdecacf2018-02-02 20:32:49 -050097 SkASSERT(width > 0);
98 SkASSERT(height > 0);
99 SkASSERT(sampleCnt > 0);
100 SkASSERT(1 == sampleCnt || isRenderTarget);
bsalomon7775c852014-12-30 12:50:52 -0800101
cblume55f2d2d2016-02-26 13:20:48 -0800102 // make sure desc.fConfig fits in 5 bits
103 SkASSERT(sk_float_log2(kLast_GrPixelConfig) <= 5);
Brian Salomond34edf32017-05-19 15:45:48 -0400104 SkASSERT(static_cast<int>(config) < (1 << 5));
105 SkASSERT(sampleCnt < (1 << 8));
bsalomon7775c852014-12-30 12:50:52 -0800106 SkASSERT(flags < (1 << 10));
Greg Daniele252f082017-10-23 16:05:23 -0400107 SkASSERT(static_cast<int>(mipMapped) <= 1);
bsalomon7775c852014-12-30 12:50:52 -0800108
joshualittb90de312015-10-01 11:54:34 -0700109 GrScratchKey::Builder builder(key, kType, 3);
Brian Salomond34edf32017-05-19 15:45:48 -0400110 builder[0] = width;
111 builder[1] = height;
Greg Daniele252f082017-10-23 16:05:23 -0400112 builder[2] = config | (static_cast<uint8_t>(mipMapped) << 5) | (sampleCnt << 6) | (flags << 14);
Brian Salomond34edf32017-05-19 15:45:48 -0400113}
114
Greg Daniel21918232017-09-08 14:46:23 -0400115void GrTexturePriv::ComputeScratchKey(const GrSurfaceDesc& desc, GrScratchKey* key) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400116 // Note: the fOrigin field is not used in the scratch key
117 return ComputeScratchKey(desc.fConfig, desc.fWidth, desc.fHeight,
Brian Salomond34edf32017-05-19 15:45:48 -0400118 SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag), desc.fSampleCnt,
Greg Daniele252f082017-10-23 16:05:23 -0400119 GrMipMapped::kNo, key);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000120}