blob: 4b43fc8513765923b0cb9b302649c6b03c8b8e76 [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"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000010#include "GrDrawTargetCaps.h"
bsalomon@google.com05ef5102011-05-02 21:14:59 +000011#include "GrGpu.h"
bsalomon7775c852014-12-30 12:50:52 -080012#include "GrResourceKey.h"
bsalomonafbf2d62014-09-30 12:18:44 -070013#include "GrTexture.h"
14#include "GrTexturePriv.h"
bsalomon@google.com8295dc12011-05-02 12:53:34 +000015
bsalomonafbf2d62014-09-30 12:18:44 -070016void GrTexture::dirtyMipMaps(bool mipMapsDirty) {
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000017 if (mipMapsDirty) {
18 if (kValid_MipMapsStatus == fMipMapsStatus) {
bsalomonafbf2d62014-09-30 12:18:44 -070019 fMipMapsStatus = kAllocated_MipMapsStatus;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000020 }
21 } else {
22 const bool sizeChanged = kNotAllocated_MipMapsStatus == fMipMapsStatus;
23 fMipMapsStatus = kValid_MipMapsStatus;
24 if (sizeChanged) {
25 // This must not be called until after changing fMipMapsStatus.
26 this->didChangeGpuMemorySize();
27 }
28 }
29}
30
bsalomon69ed47f2014-11-12 11:13:39 -080031size_t GrTexture::onGpuMemorySize() const {
bsalomond4cb9222014-08-11 14:19:09 -070032 size_t textureSize;
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +000033
34 if (GrPixelConfigIsCompressed(fDesc.fConfig)) {
krajcevski7ef21622014-07-16 15:21:13 -070035 textureSize = GrCompressedFormatDataSize(fDesc.fConfig, fDesc.fWidth, fDesc.fHeight);
bsalomond4cb9222014-08-11 14:19:09 -070036 } else {
37 textureSize = (size_t) fDesc.fWidth * fDesc.fHeight * GrBytesPerPixel(fDesc.fConfig);
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +000038 }
39
bsalomonafbf2d62014-09-30 12:18:44 -070040 if (this->texturePriv().hasMipMaps()) {
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000041 // We don't have to worry about the mipmaps being a different size than
42 // we'd expect because we never change fDesc.fWidth/fHeight.
43 textureSize *= 2;
44 }
45 return textureSize;
46}
47
robertphillips@google.com32716282012-06-04 12:48:45 +000048void GrTexture::validateDesc() const {
bsalomon49f085d2014-09-05 13:34:00 -070049 if (this->asRenderTarget()) {
robertphillips@google.com32716282012-06-04 12:48:45 +000050 // This texture has a render target
bsalomonf2703d82014-10-28 14:33:06 -070051 SkASSERT(0 != (fDesc.fFlags & kRenderTarget_GrSurfaceFlag));
robertphillips@google.com32716282012-06-04 12:48:45 +000052
bsalomon49f085d2014-09-05 13:34:00 -070053 if (this->asRenderTarget()->getStencilBuffer()) {
bsalomonf2703d82014-10-28 14:33:06 -070054 SkASSERT(0 != (fDesc.fFlags & kNoStencil_GrSurfaceFlag));
robertphillips@google.com32716282012-06-04 12:48:45 +000055 } else {
bsalomonf2703d82014-10-28 14:33:06 -070056 SkASSERT(0 == (fDesc.fFlags & kNoStencil_GrSurfaceFlag));
robertphillips@google.com32716282012-06-04 12:48:45 +000057 }
58
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000059 SkASSERT(fDesc.fSampleCnt == this->asRenderTarget()->numSamples());
robertphillips@google.com32716282012-06-04 12:48:45 +000060 } else {
bsalomonf2703d82014-10-28 14:33:06 -070061 SkASSERT(0 == (fDesc.fFlags & kRenderTarget_GrSurfaceFlag));
62 SkASSERT(0 == (fDesc.fFlags & kNoStencil_GrSurfaceFlag));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000063 SkASSERT(0 == fDesc.fSampleCnt);
robertphillips@google.com32716282012-06-04 12:48:45 +000064 }
65}
robertphillips@google.coma1e57952012-06-04 20:05:28 +000066
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000067//////////////////////////////////////////////////////////////////////////////
68
robertphillips@google.coma1e57952012-06-04 20:05:28 +000069namespace {
robertphillips@google.coma1e57952012-06-04 20:05:28 +000070
jvanverth39edf762014-12-22 11:44:19 -080071// FIXME: This should be refactored with the code in gl/GrGLGpu.cpp.
bsalomonf2703d82014-10-28 14:33:06 -070072GrSurfaceOrigin resolve_origin(const GrSurfaceDesc& desc) {
senorblanco@chromium.org709906b2013-02-07 00:35:25 +000073 // By default, GrRenderTargets are GL's normal orientation so that they
74 // can be drawn to by the outside world without the client having
75 // to render upside down.
bsalomonf2703d82014-10-28 14:33:06 -070076 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrSurfaceFlag);
senorblanco@chromium.org709906b2013-02-07 00:35:25 +000077 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
78 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
79 } else {
80 return desc.fOrigin;
81 }
82}
robertphillips@google.coma1e57952012-06-04 20:05:28 +000083}
84
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000085//////////////////////////////////////////////////////////////////////////////
bsalomon5236cf42015-01-14 10:42:08 -080086GrTexture::GrTexture(GrGpu* gpu, LifeCycle lifeCycle, const GrSurfaceDesc& desc)
87 : INHERITED(gpu, lifeCycle, desc)
bsalomon744998e2014-08-28 09:54:34 -070088 , fMipMapsStatus(kNotAllocated_MipMapsStatus) {
bsalomon8b79d232014-11-10 10:19:06 -080089
bsalomond2a6f4e2015-02-04 10:55:54 -080090 if (kWrapped_LifeCycle != lifeCycle && !GrPixelConfigIsCompressed(desc.fConfig)) {
bsalomon7775c852014-12-30 12:50:52 -080091 GrScratchKey key;
92 GrTexturePriv::ComputeScratchKey(desc, &key);
93 this->setScratchKey(key);
bsalomon8b79d232014-11-10 10:19:06 -080094 }
bsalomonafbf2d62014-09-30 12:18:44 -070095 // only make sense if alloc size is pow2
96 fShiftFixedX = 31 - SkCLZ(fDesc.fWidth);
97 fShiftFixedY = 31 - SkCLZ(fDesc.fHeight);
bsalomon744998e2014-08-28 09:54:34 -070098}
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000099
bsalomon7775c852014-12-30 12:50:52 -0800100void GrTexturePriv::ComputeScratchKey(const GrSurfaceDesc& desc, GrScratchKey* key) {
101 static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType();
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000102
bsalomon7775c852014-12-30 12:50:52 -0800103 GrScratchKey::Builder builder(key, kType, 2);
104
105 GrSurfaceOrigin origin = resolve_origin(desc);
106 uint32_t flags = desc.fFlags & ~kCheckAllocation_GrSurfaceFlag;
107
108 SkASSERT(desc.fWidth <= SK_MaxU16);
109 SkASSERT(desc.fHeight <= SK_MaxU16);
110 SkASSERT(static_cast<int>(desc.fConfig) < (1 << 6));
111 SkASSERT(desc.fSampleCnt < (1 << 8));
112 SkASSERT(flags < (1 << 10));
113 SkASSERT(static_cast<int>(origin) < (1 << 8));
114
115 builder[0] = desc.fWidth | (desc.fHeight << 16);
116 builder[1] = desc.fConfig | (desc.fSampleCnt << 6) | (flags << 14) | (origin << 24);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000117}