blob: bd8ad3b6d8808c6343f69771052027a9207e14a5 [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/////////////////////////////////////////////////////////////////////////////
Brian Salomon60dd8c72018-07-30 10:24:13 -040038GrTexture::GrTexture(GrGpu* gpu, const GrSurfaceDesc& desc, GrTextureType textureType,
Brian Salomone632dfc2018-08-01 13:01:16 -040039 GrMipMapsStatus mipMapsStatus)
40 : INHERITED(gpu, desc), fTextureType(textureType), fMipMapsStatus(mipMapsStatus) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040041 if (GrMipMapsStatus::kNotAllocated == fMipMapsStatus) {
cblume55f2d2d2016-02-26 13:20:48 -080042 fMaxMipMapLevel = 0;
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040043 } else {
44 fMaxMipMapLevel = SkMipMap::ComputeLevelCount(this->width(), this->height());
cblume55f2d2d2016-02-26 13:20:48 -080045 }
bsalomon744998e2014-08-28 09:54:34 -070046}
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000047
Eric Karl914a36b2017-10-12 12:44:50 -070048bool GrTexture::StealBackendTexture(sk_sp<GrTexture>&& texture,
49 GrBackendTexture* backendTexture,
50 SkImage::BackendTextureReleaseProc* releaseProc) {
51 if (!texture->surfacePriv().hasUniqueRef() || texture->surfacePriv().hasPendingIO()) {
52 return false;
53 }
54
55 if (!texture->onStealBackendTexture(backendTexture, releaseProc)) {
56 return false;
57 }
58
59 // Release any not-stolen data being held by this class.
60 texture->onRelease();
61 // Abandon the GrTexture so it can't be re-used.
62 texture->abandon();
63
64 return true;
65}
66
kkinnunen2e6055b2016-04-22 01:48:29 -070067void GrTexture::computeScratchKey(GrScratchKey* key) const {
Robert Phillips92de6312017-05-23 07:43:48 -040068 const GrRenderTarget* rt = this->asRenderTarget();
Brian Salomonbdecacf2018-02-02 20:32:49 -050069 int sampleCount = 1;
Robert Phillips92de6312017-05-23 07:43:48 -040070 if (rt) {
71 sampleCount = rt->numStencilSamples();
kkinnunen2e6055b2016-04-22 01:48:29 -070072 }
Robert Phillips92de6312017-05-23 07:43:48 -040073 GrTexturePriv::ComputeScratchKey(this->config(), this->width(), this->height(),
Robert Phillipsb0e93a22017-08-29 08:26:54 -040074 SkToBool(rt), sampleCount,
Greg Daniele252f082017-10-23 16:05:23 -040075 this->texturePriv().mipMapped(), key);
kkinnunen2e6055b2016-04-22 01:48:29 -070076}
77
Brian Salomond34edf32017-05-19 15:45:48 -040078void GrTexturePriv::ComputeScratchKey(GrPixelConfig config, int width, int height,
Robert Phillipsb0e93a22017-08-29 08:26:54 -040079 bool isRenderTarget, int sampleCnt,
Greg Daniele252f082017-10-23 16:05:23 -040080 GrMipMapped mipMapped, GrScratchKey* key) {
bsalomon7775c852014-12-30 12:50:52 -080081 static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType();
Brian Salomond34edf32017-05-19 15:45:48 -040082 uint32_t flags = isRenderTarget;
Brian Salomonbdecacf2018-02-02 20:32:49 -050083 SkASSERT(width > 0);
84 SkASSERT(height > 0);
85 SkASSERT(sampleCnt > 0);
86 SkASSERT(1 == sampleCnt || isRenderTarget);
bsalomon7775c852014-12-30 12:50:52 -080087
cblume55f2d2d2016-02-26 13:20:48 -080088 // make sure desc.fConfig fits in 5 bits
89 SkASSERT(sk_float_log2(kLast_GrPixelConfig) <= 5);
Brian Salomond34edf32017-05-19 15:45:48 -040090 SkASSERT(static_cast<int>(config) < (1 << 5));
91 SkASSERT(sampleCnt < (1 << 8));
bsalomon7775c852014-12-30 12:50:52 -080092 SkASSERT(flags < (1 << 10));
Greg Daniele252f082017-10-23 16:05:23 -040093 SkASSERT(static_cast<int>(mipMapped) <= 1);
bsalomon7775c852014-12-30 12:50:52 -080094
joshualittb90de312015-10-01 11:54:34 -070095 GrScratchKey::Builder builder(key, kType, 3);
Brian Salomond34edf32017-05-19 15:45:48 -040096 builder[0] = width;
97 builder[1] = height;
Greg Daniele252f082017-10-23 16:05:23 -040098 builder[2] = config | (static_cast<uint8_t>(mipMapped) << 5) | (sampleCnt << 6) | (flags << 14);
Brian Salomond34edf32017-05-19 15:45:48 -040099}
100
Greg Daniel21918232017-09-08 14:46:23 -0400101void GrTexturePriv::ComputeScratchKey(const GrSurfaceDesc& desc, GrScratchKey* key) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400102 // Note: the fOrigin field is not used in the scratch key
103 return ComputeScratchKey(desc.fConfig, desc.fWidth, desc.fHeight,
Brian Salomond34edf32017-05-19 15:45:48 -0400104 SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag), desc.fSampleCnt,
Greg Daniele252f082017-10-23 16:05:23 -0400105 GrMipMapped::kNo, key);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000106}