blob: 0051a225efbfdeba44c2083ed985e8756a261e72 [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"
Eric Karl914a36b2017-10-12 12:44:50 -070013#include "GrSurfacePriv.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
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040021void GrTexture::markMipMapsDirty() {
22 if (GrMipMapsStatus::kValid == fMipMapsStatus) {
23 fMipMapsStatus = GrMipMapsStatus::kDirty;
24 }
25}
26
27void GrTexture::markMipMapsClean() {
Greg Daniel09c94002018-06-08 22:11:51 +000028 SkASSERT(GrMipMapsStatus::kNotAllocated != fMipMapsStatus);
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040029 fMipMapsStatus = GrMipMapsStatus::kValid;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000030}
31
Robert Phillips29e52f12016-11-03 10:19:14 -040032size_t GrTexture::onGpuMemorySize() const {
Brian Salomonbb5711a2017-05-17 13:49:59 -040033 return GrSurface::ComputeSize(this->config(), this->width(), this->height(), 1,
Greg Daniele252f082017-10-23 16:05:23 -040034 this->texturePriv().mipMapped(), false);
Robert Phillips29e52f12016-11-03 10:19:14 -040035}
36
Brian Salomond34edf32017-05-19 15:45:48 -040037/////////////////////////////////////////////////////////////////////////////
kkinnunen2e6055b2016-04-22 01:48:29 -070038GrTexture::GrTexture(GrGpu* gpu, const GrSurfaceDesc& desc, GrSLType samplerType,
Greg Daniel834f1202017-10-09 15:06:20 -040039 GrSamplerState::Filter highestFilterMode,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040040 GrMipMapsStatus mipMapsStatus)
Brian Salomon2bbdcc42017-09-07 12:36:34 -040041 : INHERITED(gpu, desc)
42 , fSamplerType(samplerType)
43 , fHighestFilterMode(highestFilterMode)
Brian Osman2b23c4b2018-06-01 12:25:08 -040044 , fMipMapsStatus(mipMapsStatus) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040045 if (GrMipMapsStatus::kNotAllocated == fMipMapsStatus) {
cblume55f2d2d2016-02-26 13:20:48 -080046 fMaxMipMapLevel = 0;
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040047 } else {
48 fMaxMipMapLevel = SkMipMap::ComputeLevelCount(this->width(), this->height());
cblume55f2d2d2016-02-26 13:20:48 -080049 }
bsalomon744998e2014-08-28 09:54:34 -070050}
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000051
Eric Karl914a36b2017-10-12 12:44:50 -070052bool GrTexture::StealBackendTexture(sk_sp<GrTexture>&& texture,
53 GrBackendTexture* backendTexture,
54 SkImage::BackendTextureReleaseProc* releaseProc) {
55 if (!texture->surfacePriv().hasUniqueRef() || texture->surfacePriv().hasPendingIO()) {
56 return false;
57 }
58
59 if (!texture->onStealBackendTexture(backendTexture, releaseProc)) {
60 return false;
61 }
62
63 // Release any not-stolen data being held by this class.
64 texture->onRelease();
65 // Abandon the GrTexture so it can't be re-used.
66 texture->abandon();
67
68 return true;
69}
70
kkinnunen2e6055b2016-04-22 01:48:29 -070071void GrTexture::computeScratchKey(GrScratchKey* key) const {
Robert Phillips92de6312017-05-23 07:43:48 -040072 const GrRenderTarget* rt = this->asRenderTarget();
Brian Salomonbdecacf2018-02-02 20:32:49 -050073 int sampleCount = 1;
Robert Phillips92de6312017-05-23 07:43:48 -040074 if (rt) {
75 sampleCount = rt->numStencilSamples();
kkinnunen2e6055b2016-04-22 01:48:29 -070076 }
Robert Phillips92de6312017-05-23 07:43:48 -040077 GrTexturePriv::ComputeScratchKey(this->config(), this->width(), this->height(),
Robert Phillipsb0e93a22017-08-29 08:26:54 -040078 SkToBool(rt), sampleCount,
Greg Daniele252f082017-10-23 16:05:23 -040079 this->texturePriv().mipMapped(), key);
kkinnunen2e6055b2016-04-22 01:48:29 -070080}
81
Brian Salomond34edf32017-05-19 15:45:48 -040082void GrTexturePriv::ComputeScratchKey(GrPixelConfig config, int width, int height,
Robert Phillipsb0e93a22017-08-29 08:26:54 -040083 bool isRenderTarget, int sampleCnt,
Greg Daniele252f082017-10-23 16:05:23 -040084 GrMipMapped mipMapped, GrScratchKey* key) {
bsalomon7775c852014-12-30 12:50:52 -080085 static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType();
Brian Salomond34edf32017-05-19 15:45:48 -040086 uint32_t flags = isRenderTarget;
Brian Salomonbdecacf2018-02-02 20:32:49 -050087 SkASSERT(width > 0);
88 SkASSERT(height > 0);
89 SkASSERT(sampleCnt > 0);
90 SkASSERT(1 == sampleCnt || isRenderTarget);
bsalomon7775c852014-12-30 12:50:52 -080091
cblume55f2d2d2016-02-26 13:20:48 -080092 // make sure desc.fConfig fits in 5 bits
93 SkASSERT(sk_float_log2(kLast_GrPixelConfig) <= 5);
Brian Salomond34edf32017-05-19 15:45:48 -040094 SkASSERT(static_cast<int>(config) < (1 << 5));
95 SkASSERT(sampleCnt < (1 << 8));
bsalomon7775c852014-12-30 12:50:52 -080096 SkASSERT(flags < (1 << 10));
Greg Daniele252f082017-10-23 16:05:23 -040097 SkASSERT(static_cast<int>(mipMapped) <= 1);
bsalomon7775c852014-12-30 12:50:52 -080098
joshualittb90de312015-10-01 11:54:34 -070099 GrScratchKey::Builder builder(key, kType, 3);
Brian Salomond34edf32017-05-19 15:45:48 -0400100 builder[0] = width;
101 builder[1] = height;
Greg Daniele252f082017-10-23 16:05:23 -0400102 builder[2] = config | (static_cast<uint8_t>(mipMapped) << 5) | (sampleCnt << 6) | (flags << 14);
Brian Salomond34edf32017-05-19 15:45:48 -0400103}
104
Greg Daniel21918232017-09-08 14:46:23 -0400105void GrTexturePriv::ComputeScratchKey(const GrSurfaceDesc& desc, GrScratchKey* key) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400106 // Note: the fOrigin field is not used in the scratch key
107 return ComputeScratchKey(desc.fConfig, desc.fWidth, desc.fHeight,
Brian Salomond34edf32017-05-19 15:45:48 -0400108 SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag), desc.fSampleCnt,
Greg Daniele252f082017-10-23 16:05:23 -0400109 GrMipMapped::kNo, key);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000110}