blob: fe5b2c3c375d9b2131eb1ba1a43d39cea0d74f14 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
bsalomon@google.com669fdc42011-04-05 17:08:27 +00007 */
8
bsalomon@google.com669fdc42011-04-05 17:08:27 +00009#include "GrContext.h"
bsalomoneb1cb5c2015-05-22 08:01:09 -070010#include "GrCaps.h"
bsalomon@google.com05ef5102011-05-02 21:14:59 +000011#include "GrGpu.h"
bsalomon7775c852014-12-30 12:50:52 -080012#include "GrResourceKey.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080013#include "GrRenderTarget.h"
14#include "GrRenderTargetPriv.h"
bsalomonafbf2d62014-09-30 12:18:44 -070015#include "GrTexture.h"
16#include "GrTexturePriv.h"
bsalomon@google.com8295dc12011-05-02 12:53:34 +000017
bsalomonafbf2d62014-09-30 12:18:44 -070018void GrTexture::dirtyMipMaps(bool mipMapsDirty) {
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000019 if (mipMapsDirty) {
20 if (kValid_MipMapsStatus == fMipMapsStatus) {
cblume61214052016-01-26 09:10:48 -080021 fMipMapsStatus = kAllocated_MipMapsStatus;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000022 }
23 } else {
24 const bool sizeChanged = kNotAllocated_MipMapsStatus == fMipMapsStatus;
25 fMipMapsStatus = kValid_MipMapsStatus;
26 if (sizeChanged) {
27 // This must not be called until after changing fMipMapsStatus.
28 this->didChangeGpuMemorySize();
29 }
30 }
31}
32
bsalomon69ed47f2014-11-12 11:13:39 -080033size_t GrTexture::onGpuMemorySize() const {
bsalomond4cb9222014-08-11 14:19:09 -070034 size_t textureSize;
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +000035
36 if (GrPixelConfigIsCompressed(fDesc.fConfig)) {
krajcevski7ef21622014-07-16 15:21:13 -070037 textureSize = GrCompressedFormatDataSize(fDesc.fConfig, fDesc.fWidth, fDesc.fHeight);
bsalomond4cb9222014-08-11 14:19:09 -070038 } else {
39 textureSize = (size_t) fDesc.fWidth * fDesc.fHeight * GrBytesPerPixel(fDesc.fConfig);
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +000040 }
41
bsalomonafbf2d62014-09-30 12:18:44 -070042 if (this->texturePriv().hasMipMaps()) {
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000043 // We don't have to worry about the mipmaps being a different size than
44 // we'd expect because we never change fDesc.fWidth/fHeight.
robertphillips6e83ac72015-08-13 05:19:14 -070045 textureSize += textureSize/3;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000046 }
robertphillips6e83ac72015-08-13 05:19:14 -070047
48 SkASSERT(!SkToBool(fDesc.fFlags & kRenderTarget_GrSurfaceFlag));
49 SkASSERT(textureSize <= WorseCaseSize(fDesc));
50
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000051 return textureSize;
52}
53
robertphillips@google.com32716282012-06-04 12:48:45 +000054void GrTexture::validateDesc() const {
bsalomon49f085d2014-09-05 13:34:00 -070055 if (this->asRenderTarget()) {
robertphillips@google.com32716282012-06-04 12:48:45 +000056 // This texture has a render target
bsalomonf2703d82014-10-28 14:33:06 -070057 SkASSERT(0 != (fDesc.fFlags & kRenderTarget_GrSurfaceFlag));
vbuzinovdded6962015-06-12 08:59:45 -070058 SkASSERT(fDesc.fSampleCnt == this->asRenderTarget()->numColorSamples());
robertphillips@google.com32716282012-06-04 12:48:45 +000059 } else {
bsalomonf2703d82014-10-28 14:33:06 -070060 SkASSERT(0 == (fDesc.fFlags & kRenderTarget_GrSurfaceFlag));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000061 SkASSERT(0 == fDesc.fSampleCnt);
robertphillips@google.com32716282012-06-04 12:48:45 +000062 }
63}
robertphillips@google.coma1e57952012-06-04 20:05:28 +000064
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000065//////////////////////////////////////////////////////////////////////////////
66
robertphillips@google.coma1e57952012-06-04 20:05:28 +000067namespace {
robertphillips@google.coma1e57952012-06-04 20:05:28 +000068
jvanverth39edf762014-12-22 11:44:19 -080069// FIXME: This should be refactored with the code in gl/GrGLGpu.cpp.
bsalomonf2703d82014-10-28 14:33:06 -070070GrSurfaceOrigin resolve_origin(const GrSurfaceDesc& desc) {
senorblanco@chromium.org709906b2013-02-07 00:35:25 +000071 // By default, GrRenderTargets are GL's normal orientation so that they
72 // can be drawn to by the outside world without the client having
73 // to render upside down.
bsalomonf2703d82014-10-28 14:33:06 -070074 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrSurfaceFlag);
senorblanco@chromium.org709906b2013-02-07 00:35:25 +000075 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
76 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
77 } else {
78 return desc.fOrigin;
79 }
80}
robertphillips@google.coma1e57952012-06-04 20:05:28 +000081}
82
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000083//////////////////////////////////////////////////////////////////////////////
bsalomon5236cf42015-01-14 10:42:08 -080084GrTexture::GrTexture(GrGpu* gpu, LifeCycle lifeCycle, const GrSurfaceDesc& desc)
85 : INHERITED(gpu, lifeCycle, desc)
bsalomon744998e2014-08-28 09:54:34 -070086 , fMipMapsStatus(kNotAllocated_MipMapsStatus) {
bsalomon8b79d232014-11-10 10:19:06 -080087
erikchen7fec91c2016-02-05 12:10:55 -080088 if (!this->isExternal() && !GrPixelConfigIsCompressed(desc.fConfig) &&
89 !desc.fTextureStorageAllocator.fAllocateTextureStorage) {
bsalomon7775c852014-12-30 12:50:52 -080090 GrScratchKey key;
91 GrTexturePriv::ComputeScratchKey(desc, &key);
92 this->setScratchKey(key);
bsalomon8b79d232014-11-10 10:19:06 -080093 }
bsalomonafbf2d62014-09-30 12:18:44 -070094 // only make sense if alloc size is pow2
95 fShiftFixedX = 31 - SkCLZ(fDesc.fWidth);
96 fShiftFixedY = 31 - SkCLZ(fDesc.fHeight);
bsalomon744998e2014-08-28 09:54:34 -070097}
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000098
bsalomon7775c852014-12-30 12:50:52 -080099void GrTexturePriv::ComputeScratchKey(const GrSurfaceDesc& desc, GrScratchKey* key) {
100 static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType();
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000101
bsalomon7775c852014-12-30 12:50:52 -0800102 GrSurfaceOrigin origin = resolve_origin(desc);
103 uint32_t flags = desc.fFlags & ~kCheckAllocation_GrSurfaceFlag;
104
bsalomon7775c852014-12-30 12:50:52 -0800105 SkASSERT(static_cast<int>(desc.fConfig) < (1 << 6));
106 SkASSERT(desc.fSampleCnt < (1 << 8));
107 SkASSERT(flags < (1 << 10));
108 SkASSERT(static_cast<int>(origin) < (1 << 8));
109
joshualittb90de312015-10-01 11:54:34 -0700110 GrScratchKey::Builder builder(key, kType, 3);
111 builder[0] = desc.fWidth;
112 builder[1] = desc.fHeight;
113 builder[2] = desc.fConfig | (desc.fSampleCnt << 6) | (flags << 14) | (origin << 24);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000114}