blob: c5cd4b10c13d4321149f90cb17f0dccd442eb07c [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
8#include "GrSurface.h"
bsalomonf276ac52015-10-09 13:36:42 -07009#include "GrContext.h"
Robert Phillipsf2361d22016-10-25 14:20:06 -040010#include "GrOpList.h"
Robert Phillipsc4f0a822017-06-13 08:11:36 -040011#include "GrRenderTarget.h"
Robert Phillipsf8e25022017-11-08 15:24:31 -050012#include "GrResourceProvider.h"
bsalomonafbf2d62014-09-30 12:18:44 -070013#include "GrSurfacePriv.h"
Mike Reed84dd8572017-03-08 22:21:00 -050014#include "GrTexture.h"
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000015
Brian Osman3b655982017-03-07 16:58:08 -050016#include "SkGr.h"
Robert Phillipsb4460882016-11-17 14:43:51 -050017#include "SkMathPriv.h"
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +000018
Robert Phillipsb4460882016-11-17 14:43:51 -050019size_t GrSurface::WorstCaseSize(const GrSurfaceDesc& desc, bool useNextPow2) {
robertphillips6e83ac72015-08-13 05:19:14 -070020 size_t size;
21
Robert Phillipsf8e25022017-11-08 15:24:31 -050022 int width = useNextPow2
23 ? SkTMax(GrResourceProvider::kMinScratchTextureSize, GrNextPow2(desc.fWidth))
24 : desc.fWidth;
25 int height = useNextPow2
26 ? SkTMax(GrResourceProvider::kMinScratchTextureSize, GrNextPow2(desc.fHeight))
27 : desc.fHeight;
Robert Phillipsb4460882016-11-17 14:43:51 -050028
robertphillips6e83ac72015-08-13 05:19:14 -070029 bool isRenderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
30 if (isRenderTarget) {
31 // We own one color value for each MSAA sample.
Brian Salomonbdecacf2018-02-02 20:32:49 -050032 SkASSERT(desc.fSampleCnt >= 1);
33 int colorValuesPerPixel = desc.fSampleCnt;
34 if (desc.fSampleCnt > 1) {
robertphillips6e83ac72015-08-13 05:19:14 -070035 // Worse case, we own the resolve buffer so that is one more sample per pixel.
36 colorValuesPerPixel += 1;
37 }
38 SkASSERT(kUnknown_GrPixelConfig != desc.fConfig);
Jim Van Verth1676cb92019-01-15 13:24:45 -050039 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig));
Robert Phillipsb4460882016-11-17 14:43:51 -050040 size_t colorBytes = (size_t) width * height * GrBytesPerPixel(desc.fConfig);
robertphillips4c56b9f2016-08-17 08:02:51 -070041
Robert Phillipsd6214d42016-11-07 08:23:48 -050042 // This would be a nice assert to have (i.e., we aren't creating 0 width/height surfaces).
43 // Unfortunately Chromium seems to want to do this.
44 //SkASSERT(colorBytes > 0);
45
46 size = colorValuesPerPixel * colorBytes;
47 size += colorBytes/3; // in case we have to mipmap
robertphillips6e83ac72015-08-13 05:19:14 -070048 } else {
Jim Van Verth1676cb92019-01-15 13:24:45 -050049 if (GrPixelConfigIsCompressed(desc.fConfig)) {
50 size = GrCompressedFormatDataSize(desc.fConfig, width, height);
51 } else {
52 size = (size_t)width * height * GrBytesPerPixel(desc.fConfig);
53 }
robertphillips6e83ac72015-08-13 05:19:14 -070054
55 size += size/3; // in case we have to mipmap
56 }
57
58 return size;
59}
60
Brian Salomonbb5711a2017-05-17 13:49:59 -040061size_t GrSurface::ComputeSize(GrPixelConfig config,
62 int width,
63 int height,
Robert Phillipsd6214d42016-11-07 08:23:48 -050064 int colorSamplesPerPixel,
Greg Daniele252f082017-10-23 16:05:23 -040065 GrMipMapped mipMapped,
Robert Phillipsb4460882016-11-17 14:43:51 -050066 bool useNextPow2) {
Jim Van Verth1676cb92019-01-15 13:24:45 -050067 size_t colorSize;
68
Robert Phillipsf8e25022017-11-08 15:24:31 -050069 width = useNextPow2
70 ? SkTMax(GrResourceProvider::kMinScratchTextureSize, GrNextPow2(width))
71 : width;
72 height = useNextPow2
73 ? SkTMax(GrResourceProvider::kMinScratchTextureSize, GrNextPow2(height))
74 : height;
Robert Phillipsb4460882016-11-17 14:43:51 -050075
Brian Salomonbb5711a2017-05-17 13:49:59 -040076 SkASSERT(kUnknown_GrPixelConfig != config);
Jim Van Verth1676cb92019-01-15 13:24:45 -050077 if (GrPixelConfigIsCompressed(config)) {
78 colorSize = GrCompressedFormatDataSize(config, width, height);
79 } else {
80 colorSize = (size_t)width * height * GrBytesPerPixel(config);
81 }
Robert Phillipsd6214d42016-11-07 08:23:48 -050082 SkASSERT(colorSize > 0);
83
84 size_t finalSize = colorSamplesPerPixel * colorSize;
85
Greg Daniele252f082017-10-23 16:05:23 -040086 if (GrMipMapped::kYes == mipMapped) {
Robert Phillipsd6214d42016-11-07 08:23:48 -050087 // We don't have to worry about the mipmaps being a different size than
88 // we'd expect because we never change fDesc.fWidth/fHeight.
89 finalSize += colorSize/3;
90 }
Robert Phillipsd6214d42016-11-07 08:23:48 -050091 return finalSize;
92}
93
bsalomone8d21e82015-07-16 08:23:13 -070094template<typename T> static bool adjust_params(int surfaceWidth,
95 int surfaceHeight,
96 size_t bpp,
97 int* left, int* top, int* width, int* height,
98 T** data,
99 size_t* rowBytes) {
100 if (!*rowBytes) {
101 *rowBytes = *width * bpp;
102 }
103
104 SkIRect subRect = SkIRect::MakeXYWH(*left, *top, *width, *height);
105 SkIRect bounds = SkIRect::MakeWH(surfaceWidth, surfaceHeight);
106
107 if (!subRect.intersect(bounds)) {
108 return false;
109 }
110 *data = reinterpret_cast<void*>(reinterpret_cast<intptr_t>(*data) +
111 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
112
113 *left = subRect.fLeft;
114 *top = subRect.fTop;
115 *width = subRect.width();
116 *height = subRect.height();
117 return true;
118}
119
120bool GrSurfacePriv::AdjustReadPixelParams(int surfaceWidth,
121 int surfaceHeight,
122 size_t bpp,
123 int* left, int* top, int* width, int* height,
124 void** data,
125 size_t* rowBytes) {
126 return adjust_params<void>(surfaceWidth, surfaceHeight, bpp, left, top, width, height, data,
127 rowBytes);
128}
129
130bool GrSurfacePriv::AdjustWritePixelParams(int surfaceWidth,
131 int surfaceHeight,
132 size_t bpp,
133 int* left, int* top, int* width, int* height,
134 const void** data,
135 size_t* rowBytes) {
136 return adjust_params<const void>(surfaceWidth, surfaceHeight, bpp, left, top, width, height,
137 data, rowBytes);
138}
139
140
141//////////////////////////////////////////////////////////////////////////////
142
bsalomon8d034a12014-09-22 12:21:08 -0700143bool GrSurface::hasPendingRead() const {
144 const GrTexture* thisTex = this->asTexture();
145 if (thisTex && thisTex->internalHasPendingRead()) {
146 return true;
147 }
148 const GrRenderTarget* thisRT = this->asRenderTarget();
149 if (thisRT && thisRT->internalHasPendingRead()) {
150 return true;
151 }
152 return false;
153}
154
155bool GrSurface::hasPendingWrite() const {
156 const GrTexture* thisTex = this->asTexture();
157 if (thisTex && thisTex->internalHasPendingWrite()) {
158 return true;
159 }
160 const GrRenderTarget* thisRT = this->asRenderTarget();
161 if (thisRT && thisRT->internalHasPendingWrite()) {
162 return true;
163 }
164 return false;
165}
166
167bool GrSurface::hasPendingIO() const {
168 const GrTexture* thisTex = this->asTexture();
169 if (thisTex && thisTex->internalHasPendingIO()) {
170 return true;
171 }
172 const GrRenderTarget* thisRT = this->asRenderTarget();
173 if (thisRT && thisRT->internalHasPendingIO()) {
174 return true;
175 }
176 return false;
177}
reedde499882015-06-18 13:41:40 -0700178
179void GrSurface::onRelease() {
Greg Daniel2d35a1c2019-02-01 14:48:10 -0500180 this->invokeReleaseProc();
reedde499882015-06-18 13:41:40 -0700181 this->INHERITED::onRelease();
182}
183
184void GrSurface::onAbandon() {
Greg Daniel2d35a1c2019-02-01 14:48:10 -0500185 this->invokeReleaseProc();
reedde499882015-06-18 13:41:40 -0700186 this->INHERITED::onAbandon();
187}