blob: 87d9e9cb0e486a08998c6650d403a4dff5495490 [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
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -040019size_t GrSurface::WorstCaseSize(const GrSurfaceDesc& desc, 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
robertphillips6e83ac72015-08-13 05:19:14 -070025 bool isRenderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
26 if (isRenderTarget) {
27 // We own one color value for each MSAA sample.
Brian Salomonbdecacf2018-02-02 20:32:49 -050028 SkASSERT(desc.fSampleCnt >= 1);
29 int colorValuesPerPixel = desc.fSampleCnt;
30 if (desc.fSampleCnt > 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 {
Jim Van Verth1676cb92019-01-15 13:24:45 -050045 if (GrPixelConfigIsCompressed(desc.fConfig)) {
46 size = GrCompressedFormatDataSize(desc.fConfig, width, height);
47 } else {
48 size = (size_t)width * height * GrBytesPerPixel(desc.fConfig);
49 }
robertphillips6e83ac72015-08-13 05:19:14 -070050
51 size += size/3; // in case we have to mipmap
52 }
53
54 return size;
55}
56
Brian Salomonbb5711a2017-05-17 13:49:59 -040057size_t GrSurface::ComputeSize(GrPixelConfig config,
58 int width,
59 int height,
Robert Phillipsd6214d42016-11-07 08:23:48 -050060 int colorSamplesPerPixel,
Greg Daniele252f082017-10-23 16:05:23 -040061 GrMipMapped mipMapped,
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -040062 bool binSize) {
Jim Van Verth1676cb92019-01-15 13:24:45 -050063 size_t colorSize;
64
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -040065 width = binSize ? GrResourceProvider::MakeApprox(width) : width;
66 height = binSize ? GrResourceProvider::MakeApprox(height) : height;
Robert Phillipsb4460882016-11-17 14:43:51 -050067
Brian Salomonbb5711a2017-05-17 13:49:59 -040068 SkASSERT(kUnknown_GrPixelConfig != config);
Jim Van Verth1676cb92019-01-15 13:24:45 -050069 if (GrPixelConfigIsCompressed(config)) {
70 colorSize = GrCompressedFormatDataSize(config, width, height);
71 } else {
72 colorSize = (size_t)width * height * GrBytesPerPixel(config);
73 }
Robert Phillipsd6214d42016-11-07 08:23:48 -050074 SkASSERT(colorSize > 0);
75
76 size_t finalSize = colorSamplesPerPixel * colorSize;
77
Greg Daniele252f082017-10-23 16:05:23 -040078 if (GrMipMapped::kYes == mipMapped) {
Robert Phillipsd6214d42016-11-07 08:23:48 -050079 // We don't have to worry about the mipmaps being a different size than
80 // we'd expect because we never change fDesc.fWidth/fHeight.
81 finalSize += colorSize/3;
82 }
Robert Phillipsd6214d42016-11-07 08:23:48 -050083 return finalSize;
84}
85
bsalomone8d21e82015-07-16 08:23:13 -070086//////////////////////////////////////////////////////////////////////////////
87
bsalomon8d034a12014-09-22 12:21:08 -070088bool GrSurface::hasPendingRead() const {
89 const GrTexture* thisTex = this->asTexture();
90 if (thisTex && thisTex->internalHasPendingRead()) {
91 return true;
92 }
93 const GrRenderTarget* thisRT = this->asRenderTarget();
94 if (thisRT && thisRT->internalHasPendingRead()) {
95 return true;
96 }
97 return false;
98}
99
100bool GrSurface::hasPendingWrite() const {
101 const GrTexture* thisTex = this->asTexture();
102 if (thisTex && thisTex->internalHasPendingWrite()) {
103 return true;
104 }
105 const GrRenderTarget* thisRT = this->asRenderTarget();
106 if (thisRT && thisRT->internalHasPendingWrite()) {
107 return true;
108 }
109 return false;
110}
111
112bool GrSurface::hasPendingIO() const {
113 const GrTexture* thisTex = this->asTexture();
114 if (thisTex && thisTex->internalHasPendingIO()) {
115 return true;
116 }
117 const GrRenderTarget* thisRT = this->asRenderTarget();
118 if (thisRT && thisRT->internalHasPendingIO()) {
119 return true;
120 }
121 return false;
122}
reedde499882015-06-18 13:41:40 -0700123
124void GrSurface::onRelease() {
Greg Daniel2d35a1c2019-02-01 14:48:10 -0500125 this->invokeReleaseProc();
reedde499882015-06-18 13:41:40 -0700126 this->INHERITED::onRelease();
127}
128
129void GrSurface::onAbandon() {
Greg Daniel2d35a1c2019-02-01 14:48:10 -0500130 this->invokeReleaseProc();
reedde499882015-06-18 13:41:40 -0700131 this->INHERITED::onAbandon();
132}