blob: db5a7078bafcace845290749e1becd496d454906 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/gpu/GrSurface.h"
10#include "include/gpu/GrTexture.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040011#include "src/gpu/GrOpList.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040012#include "src/gpu/GrRenderTarget.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 Salomon27b4d8d2019-07-22 14:23:45 -040019size_t GrSurface::WorstCaseSize(const GrSurfaceDesc& desc, GrRenderable renderable,
20 int renderTargetSampleCnt, bool binSize) {
robertphillips6e83ac72015-08-13 05:19:14 -070021 size_t size;
22
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -040023 int width = binSize ? GrResourceProvider::MakeApprox(desc.fWidth) : desc.fWidth;
24 int height = binSize ? GrResourceProvider::MakeApprox(desc.fHeight) : desc.fHeight;
Robert Phillipsb4460882016-11-17 14:43:51 -050025
Brian Salomonf2c2ba92019-07-17 09:59:59 -040026 if (renderable == GrRenderable::kYes) {
robertphillips6e83ac72015-08-13 05:19:14 -070027 // We own one color value for each MSAA sample.
Brian Salomon27b4d8d2019-07-22 14:23:45 -040028 SkASSERT(renderTargetSampleCnt >= 1);
29 int colorValuesPerPixel = renderTargetSampleCnt;
30 if (renderTargetSampleCnt > 1) {
robertphillips6e83ac72015-08-13 05:19:14 -070031 // Worse case, we own the resolve buffer so that is one more sample per pixel.
32 colorValuesPerPixel += 1;
33 }
34 SkASSERT(kUnknown_GrPixelConfig != desc.fConfig);
Jim Van Verth1676cb92019-01-15 13:24:45 -050035 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig));
Robert Phillipsb4460882016-11-17 14:43:51 -050036 size_t colorBytes = (size_t) width * height * GrBytesPerPixel(desc.fConfig);
robertphillips4c56b9f2016-08-17 08:02:51 -070037
Robert Phillipsd6214d42016-11-07 08:23:48 -050038 // This would be a nice assert to have (i.e., we aren't creating 0 width/height surfaces).
39 // Unfortunately Chromium seems to want to do this.
40 //SkASSERT(colorBytes > 0);
41
42 size = colorValuesPerPixel * colorBytes;
43 size += colorBytes/3; // in case we have to mipmap
robertphillips6e83ac72015-08-13 05:19:14 -070044 } else {
Brian Salomon27b4d8d2019-07-22 14:23:45 -040045 SkASSERT(renderTargetSampleCnt == 1);
Jim Van Verth1676cb92019-01-15 13:24:45 -050046 if (GrPixelConfigIsCompressed(desc.fConfig)) {
47 size = GrCompressedFormatDataSize(desc.fConfig, width, height);
48 } else {
49 size = (size_t)width * height * GrBytesPerPixel(desc.fConfig);
50 }
robertphillips6e83ac72015-08-13 05:19:14 -070051
52 size += size/3; // in case we have to mipmap
53 }
54
55 return size;
56}
57
Brian Salomonbb5711a2017-05-17 13:49:59 -040058size_t GrSurface::ComputeSize(GrPixelConfig config,
59 int width,
60 int height,
Robert Phillipsd6214d42016-11-07 08:23:48 -050061 int colorSamplesPerPixel,
Greg Daniele252f082017-10-23 16:05:23 -040062 GrMipMapped mipMapped,
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -040063 bool binSize) {
Jim Van Verth1676cb92019-01-15 13:24:45 -050064 size_t colorSize;
65
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -040066 width = binSize ? GrResourceProvider::MakeApprox(width) : width;
67 height = binSize ? GrResourceProvider::MakeApprox(height) : height;
Robert Phillipsb4460882016-11-17 14:43:51 -050068
Brian Salomonbb5711a2017-05-17 13:49:59 -040069 SkASSERT(kUnknown_GrPixelConfig != config);
Jim Van Verth1676cb92019-01-15 13:24:45 -050070 if (GrPixelConfigIsCompressed(config)) {
71 colorSize = GrCompressedFormatDataSize(config, width, height);
72 } else {
73 colorSize = (size_t)width * height * GrBytesPerPixel(config);
74 }
Robert Phillipsd6214d42016-11-07 08:23:48 -050075 SkASSERT(colorSize > 0);
76
77 size_t finalSize = colorSamplesPerPixel * colorSize;
78
Greg Daniele252f082017-10-23 16:05:23 -040079 if (GrMipMapped::kYes == mipMapped) {
Robert Phillipsd6214d42016-11-07 08:23:48 -050080 // We don't have to worry about the mipmaps being a different size than
81 // we'd expect because we never change fDesc.fWidth/fHeight.
82 finalSize += colorSize/3;
83 }
Robert Phillipsd6214d42016-11-07 08:23:48 -050084 return finalSize;
85}
86
bsalomone8d21e82015-07-16 08:23:13 -070087//////////////////////////////////////////////////////////////////////////////
88
bsalomon8d034a12014-09-22 12:21:08 -070089bool GrSurface::hasPendingRead() const {
90 const GrTexture* thisTex = this->asTexture();
91 if (thisTex && thisTex->internalHasPendingRead()) {
92 return true;
93 }
94 const GrRenderTarget* thisRT = this->asRenderTarget();
95 if (thisRT && thisRT->internalHasPendingRead()) {
96 return true;
97 }
98 return false;
99}
100
101bool GrSurface::hasPendingWrite() const {
102 const GrTexture* thisTex = this->asTexture();
103 if (thisTex && thisTex->internalHasPendingWrite()) {
104 return true;
105 }
106 const GrRenderTarget* thisRT = this->asRenderTarget();
107 if (thisRT && thisRT->internalHasPendingWrite()) {
108 return true;
109 }
110 return false;
111}
112
113bool GrSurface::hasPendingIO() const {
114 const GrTexture* thisTex = this->asTexture();
115 if (thisTex && thisTex->internalHasPendingIO()) {
116 return true;
117 }
118 const GrRenderTarget* thisRT = this->asRenderTarget();
119 if (thisRT && thisRT->internalHasPendingIO()) {
120 return true;
121 }
122 return false;
123}
reedde499882015-06-18 13:41:40 -0700124
125void GrSurface::onRelease() {
Greg Daniel2d35a1c2019-02-01 14:48:10 -0500126 this->invokeReleaseProc();
reedde499882015-06-18 13:41:40 -0700127 this->INHERITED::onRelease();
128}
129
130void GrSurface::onAbandon() {
Greg Daniel2d35a1c2019-02-01 14:48:10 -0500131 this->invokeReleaseProc();
reedde499882015-06-18 13:41:40 -0700132 this->INHERITED::onAbandon();
133}