blob: 67804bf4ae0c432c5cc0e694b30c5dd8fce747e6 [file] [log] [blame]
robertphillips@google.com7d501ab2012-06-21 21:09:06 +00001/*
2 * Copyright 2012 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.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/gpu/GrContext.h"
9#include "include/gpu/GrRenderTarget.h"
10#include "include/gpu/GrSurface.h"
11#include "include/gpu/GrTexture.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040012#include "src/gpu/GrOpList.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrResourceProvider.h"
14#include "src/gpu/GrSurfacePriv.h"
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000015
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/core/SkMathPriv.h"
17#include "src/gpu/SkGr.h"
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +000018
Brian Salomonf2c2ba92019-07-17 09:59:59 -040019size_t GrSurface::WorstCaseSize(const GrSurfaceDesc& desc, GrRenderable renderable, bool binSize) {
robertphillips6e83ac72015-08-13 05:19:14 -070020 size_t size;
21
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -040022 int width = binSize ? GrResourceProvider::MakeApprox(desc.fWidth) : desc.fWidth;
23 int height = binSize ? GrResourceProvider::MakeApprox(desc.fHeight) : desc.fHeight;
Robert Phillipsb4460882016-11-17 14:43:51 -050024
Brian Salomonf2c2ba92019-07-17 09:59:59 -040025 if (renderable == GrRenderable::kYes) {
robertphillips6e83ac72015-08-13 05:19:14 -070026 // We own one color value for each MSAA sample.
Brian Salomonbdecacf2018-02-02 20:32:49 -050027 SkASSERT(desc.fSampleCnt >= 1);
28 int colorValuesPerPixel = desc.fSampleCnt;
29 if (desc.fSampleCnt > 1) {
robertphillips6e83ac72015-08-13 05:19:14 -070030 // Worse case, we own the resolve buffer so that is one more sample per pixel.
31 colorValuesPerPixel += 1;
32 }
33 SkASSERT(kUnknown_GrPixelConfig != desc.fConfig);
Jim Van Verth1676cb92019-01-15 13:24:45 -050034 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig));
Robert Phillipsb4460882016-11-17 14:43:51 -050035 size_t colorBytes = (size_t) width * height * GrBytesPerPixel(desc.fConfig);
robertphillips4c56b9f2016-08-17 08:02:51 -070036
Robert Phillipsd6214d42016-11-07 08:23:48 -050037 // This would be a nice assert to have (i.e., we aren't creating 0 width/height surfaces).
38 // Unfortunately Chromium seems to want to do this.
39 //SkASSERT(colorBytes > 0);
40
41 size = colorValuesPerPixel * colorBytes;
42 size += colorBytes/3; // in case we have to mipmap
robertphillips6e83ac72015-08-13 05:19:14 -070043 } else {
Jim Van Verth1676cb92019-01-15 13:24:45 -050044 if (GrPixelConfigIsCompressed(desc.fConfig)) {
45 size = GrCompressedFormatDataSize(desc.fConfig, width, height);
46 } else {
47 size = (size_t)width * height * GrBytesPerPixel(desc.fConfig);
48 }
robertphillips6e83ac72015-08-13 05:19:14 -070049
50 size += size/3; // in case we have to mipmap
51 }
52
53 return size;
54}
55
Brian Salomonbb5711a2017-05-17 13:49:59 -040056size_t GrSurface::ComputeSize(GrPixelConfig config,
57 int width,
58 int height,
Robert Phillipsd6214d42016-11-07 08:23:48 -050059 int colorSamplesPerPixel,
Greg Daniele252f082017-10-23 16:05:23 -040060 GrMipMapped mipMapped,
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -040061 bool binSize) {
Jim Van Verth1676cb92019-01-15 13:24:45 -050062 size_t colorSize;
63
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -040064 width = binSize ? GrResourceProvider::MakeApprox(width) : width;
65 height = binSize ? GrResourceProvider::MakeApprox(height) : height;
Robert Phillipsb4460882016-11-17 14:43:51 -050066
Brian Salomonbb5711a2017-05-17 13:49:59 -040067 SkASSERT(kUnknown_GrPixelConfig != config);
Jim Van Verth1676cb92019-01-15 13:24:45 -050068 if (GrPixelConfigIsCompressed(config)) {
69 colorSize = GrCompressedFormatDataSize(config, width, height);
70 } else {
71 colorSize = (size_t)width * height * GrBytesPerPixel(config);
72 }
Robert Phillipsd6214d42016-11-07 08:23:48 -050073 SkASSERT(colorSize > 0);
74
75 size_t finalSize = colorSamplesPerPixel * colorSize;
76
Greg Daniele252f082017-10-23 16:05:23 -040077 if (GrMipMapped::kYes == mipMapped) {
Robert Phillipsd6214d42016-11-07 08:23:48 -050078 // We don't have to worry about the mipmaps being a different size than
79 // we'd expect because we never change fDesc.fWidth/fHeight.
80 finalSize += colorSize/3;
81 }
Robert Phillipsd6214d42016-11-07 08:23:48 -050082 return finalSize;
83}
84
bsalomone8d21e82015-07-16 08:23:13 -070085//////////////////////////////////////////////////////////////////////////////
86
bsalomon8d034a12014-09-22 12:21:08 -070087bool GrSurface::hasPendingRead() const {
88 const GrTexture* thisTex = this->asTexture();
89 if (thisTex && thisTex->internalHasPendingRead()) {
90 return true;
91 }
92 const GrRenderTarget* thisRT = this->asRenderTarget();
93 if (thisRT && thisRT->internalHasPendingRead()) {
94 return true;
95 }
96 return false;
97}
98
99bool GrSurface::hasPendingWrite() const {
100 const GrTexture* thisTex = this->asTexture();
101 if (thisTex && thisTex->internalHasPendingWrite()) {
102 return true;
103 }
104 const GrRenderTarget* thisRT = this->asRenderTarget();
105 if (thisRT && thisRT->internalHasPendingWrite()) {
106 return true;
107 }
108 return false;
109}
110
111bool GrSurface::hasPendingIO() const {
112 const GrTexture* thisTex = this->asTexture();
113 if (thisTex && thisTex->internalHasPendingIO()) {
114 return true;
115 }
116 const GrRenderTarget* thisRT = this->asRenderTarget();
117 if (thisRT && thisRT->internalHasPendingIO()) {
118 return true;
119 }
120 return false;
121}
reedde499882015-06-18 13:41:40 -0700122
123void GrSurface::onRelease() {
Greg Daniel2d35a1c2019-02-01 14:48:10 -0500124 this->invokeReleaseProc();
reedde499882015-06-18 13:41:40 -0700125 this->INHERITED::onRelease();
126}
127
128void GrSurface::onAbandon() {
Greg Daniel2d35a1c2019-02-01 14:48:10 -0500129 this->invokeReleaseProc();
reedde499882015-06-18 13:41:40 -0700130 this->INHERITED::onAbandon();
131}