blob: c3dacaa9a0ff4bad8a1545fa5e8990d434352869 [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"
11#include "include/gpu/GrRenderTarget.h"
12#include "include/gpu/GrTexture.h"
13#include "include/gpu/GrTypes.h"
14#include "include/private/GrResourceKey.h"
15#include "src/core/SkMipMap.h"
16#include "src/gpu/GrCaps.h"
17#include "src/gpu/GrContextPriv.h"
18#include "src/gpu/GrGpu.h"
19#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 {
Brian Salomonbb5711a2017-05-17 13:49:59 -040034 return GrSurface::ComputeSize(this->config(), this->width(), this->height(), 1,
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -040035 this->texturePriv().mipMapped());
Robert Phillips29e52f12016-11-03 10:19:14 -040036}
37
Brian Salomond34edf32017-05-19 15:45:48 -040038/////////////////////////////////////////////////////////////////////////////
Brian Salomona9c22572019-08-05 12:57:09 -040039GrTexture::GrTexture(GrGpu* gpu, const SkISize& size, GrPixelConfig config, GrProtected isProtected,
Brian Salomone8a766b2019-07-19 14:24:36 -040040 GrTextureType textureType, GrMipMapsStatus mipMapsStatus)
Brian Salomona9c22572019-08-05 12:57:09 -040041 : INHERITED(gpu, size, config, isProtected)
Brian Salomone8a766b2019-07-19 14:24:36 -040042 , fTextureType(textureType)
43 , fMipMapsStatus(mipMapsStatus) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040044 if (GrMipMapsStatus::kNotAllocated == fMipMapsStatus) {
cblume55f2d2d2016-02-26 13:20:48 -080045 fMaxMipMapLevel = 0;
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040046 } else {
47 fMaxMipMapLevel = SkMipMap::ComputeLevelCount(this->width(), this->height());
cblume55f2d2d2016-02-26 13:20:48 -080048 }
bsalomon744998e2014-08-28 09:54:34 -070049}
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000050
Brian Salomon35ba6142019-01-24 13:08:59 -050051bool GrTexture::StealBackendTexture(sk_sp<GrTexture> texture,
Eric Karl914a36b2017-10-12 12:44:50 -070052 GrBackendTexture* backendTexture,
53 SkImage::BackendTextureReleaseProc* releaseProc) {
54 if (!texture->surfacePriv().hasUniqueRef() || texture->surfacePriv().hasPendingIO()) {
55 return false;
56 }
57
58 if (!texture->onStealBackendTexture(backendTexture, releaseProc)) {
59 return false;
60 }
Brian Salomon35ba6142019-01-24 13:08:59 -050061#ifdef SK_DEBUG
Robert Phillips9da87e02019-02-04 13:26:26 -050062 GrResourceCache* cache = texture->getContext()->priv().getResourceCache();
Brian Salomon35ba6142019-01-24 13:08:59 -050063 int preCount = cache->getResourceCount();
64#endif
65 // Ensure that the texture will be released by the cache when we drop the last ref.
66 // A texture that has no refs and no keys should be immediately removed.
67 if (texture->getUniqueKey().isValid()) {
68 texture->resourcePriv().removeUniqueKey();
69 }
70 if (texture->resourcePriv().getScratchKey().isValid()) {
71 texture->resourcePriv().removeScratchKey();
72 }
73#ifdef SK_DEBUG
74 texture.reset();
75 int postCount = cache->getResourceCount();
76 SkASSERT(postCount < preCount);
77#endif
Eric Karl914a36b2017-10-12 12:44:50 -070078 return true;
79}
80
kkinnunen2e6055b2016-04-22 01:48:29 -070081void GrTexture::computeScratchKey(GrScratchKey* key) const {
Jim Van Verth1676cb92019-01-15 13:24:45 -050082 if (!GrPixelConfigIsCompressed(this->config())) {
Jim Van Verth1676cb92019-01-15 13:24:45 -050083 int sampleCount = 1;
Brian Salomonf2c2ba92019-07-17 09:59:59 -040084 GrRenderable renderable = GrRenderable::kNo;
85 if (const auto* rt = this->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -060086 sampleCount = rt->numSamples();
Brian Salomonf2c2ba92019-07-17 09:59:59 -040087 renderable = GrRenderable::kYes;
Jim Van Verth1676cb92019-01-15 13:24:45 -050088 }
Brian Salomonf2c2ba92019-07-17 09:59:59 -040089 GrTexturePriv::ComputeScratchKey(this->config(), this->width(), this->height(), renderable,
90 sampleCount, this->texturePriv().mipMapped(), key);
kkinnunen2e6055b2016-04-22 01:48:29 -070091 }
92}
93
Brian Salomond34edf32017-05-19 15:45:48 -040094void GrTexturePriv::ComputeScratchKey(GrPixelConfig config, int width, int height,
Brian Salomonf2c2ba92019-07-17 09:59:59 -040095 GrRenderable renderable, int sampleCnt, GrMipMapped mipMapped,
96 GrScratchKey* key) {
bsalomon7775c852014-12-30 12:50:52 -080097 static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType();
Brian Salomonbdecacf2018-02-02 20:32:49 -050098 SkASSERT(width > 0);
99 SkASSERT(height > 0);
100 SkASSERT(sampleCnt > 0);
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400101 SkASSERT(1 == sampleCnt || renderable == GrRenderable::kYes);
bsalomon7775c852014-12-30 12:50:52 -0800102
cblume55f2d2d2016-02-26 13:20:48 -0800103 // make sure desc.fConfig fits in 5 bits
104 SkASSERT(sk_float_log2(kLast_GrPixelConfig) <= 5);
Brian Salomond34edf32017-05-19 15:45:48 -0400105 SkASSERT(static_cast<int>(config) < (1 << 5));
106 SkASSERT(sampleCnt < (1 << 8));
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;
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400112 builder[2] = config
113 | (static_cast<uint32_t>(mipMapped) << 5)
114 | (sampleCnt << 6)
115 | (static_cast<uint32_t>(renderable) << 14);
Brian Salomond34edf32017-05-19 15:45:48 -0400116}
117
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400118void GrTexturePriv::ComputeScratchKey(const GrSurfaceDesc& desc, GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400119 int sampleCnt, GrScratchKey* key) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400120 // Note: the fOrigin field is not used in the scratch key
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400121 return ComputeScratchKey(desc.fConfig, desc.fWidth, desc.fHeight, renderable, sampleCnt,
Greg Daniele252f082017-10-23 16:05:23 -0400122 GrMipMapped::kNo, key);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000123}