blob: b35683c548beb074a2f13000faa427cc7e7aeca7 [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);
Robert Phillipsb4460882016-11-17 14:43:51 -050039 size_t colorBytes = (size_t) width * height * GrBytesPerPixel(desc.fConfig);
robertphillips4c56b9f2016-08-17 08:02:51 -070040
Robert Phillipsd6214d42016-11-07 08:23:48 -050041 // This would be a nice assert to have (i.e., we aren't creating 0 width/height surfaces).
42 // Unfortunately Chromium seems to want to do this.
43 //SkASSERT(colorBytes > 0);
44
45 size = colorValuesPerPixel * colorBytes;
46 size += colorBytes/3; // in case we have to mipmap
robertphillips6e83ac72015-08-13 05:19:14 -070047 } else {
Robert Phillips92de6312017-05-23 07:43:48 -040048 size = (size_t) width * height * GrBytesPerPixel(desc.fConfig);
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 Phillipsb4460882016-11-17 14:43:51 -050061 bool useNextPow2) {
Robert Phillipsf8e25022017-11-08 15:24:31 -050062 width = useNextPow2
63 ? SkTMax(GrResourceProvider::kMinScratchTextureSize, GrNextPow2(width))
64 : width;
65 height = useNextPow2
66 ? SkTMax(GrResourceProvider::kMinScratchTextureSize, GrNextPow2(height))
67 : height;
Robert Phillipsb4460882016-11-17 14:43:51 -050068
Brian Salomonbb5711a2017-05-17 13:49:59 -040069 SkASSERT(kUnknown_GrPixelConfig != config);
Robert Phillips92de6312017-05-23 07:43:48 -040070 size_t colorSize = (size_t)width * height * GrBytesPerPixel(config);
Robert Phillipsd6214d42016-11-07 08:23:48 -050071 SkASSERT(colorSize > 0);
72
73 size_t finalSize = colorSamplesPerPixel * colorSize;
74
Greg Daniele252f082017-10-23 16:05:23 -040075 if (GrMipMapped::kYes == mipMapped) {
Robert Phillipsd6214d42016-11-07 08:23:48 -050076 // We don't have to worry about the mipmaps being a different size than
77 // we'd expect because we never change fDesc.fWidth/fHeight.
78 finalSize += colorSize/3;
79 }
Robert Phillipsd6214d42016-11-07 08:23:48 -050080 return finalSize;
81}
82
bsalomone8d21e82015-07-16 08:23:13 -070083template<typename T> static bool adjust_params(int surfaceWidth,
84 int surfaceHeight,
85 size_t bpp,
86 int* left, int* top, int* width, int* height,
87 T** data,
88 size_t* rowBytes) {
89 if (!*rowBytes) {
90 *rowBytes = *width * bpp;
91 }
92
93 SkIRect subRect = SkIRect::MakeXYWH(*left, *top, *width, *height);
94 SkIRect bounds = SkIRect::MakeWH(surfaceWidth, surfaceHeight);
95
96 if (!subRect.intersect(bounds)) {
97 return false;
98 }
99 *data = reinterpret_cast<void*>(reinterpret_cast<intptr_t>(*data) +
100 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
101
102 *left = subRect.fLeft;
103 *top = subRect.fTop;
104 *width = subRect.width();
105 *height = subRect.height();
106 return true;
107}
108
109bool GrSurfacePriv::AdjustReadPixelParams(int surfaceWidth,
110 int surfaceHeight,
111 size_t bpp,
112 int* left, int* top, int* width, int* height,
113 void** data,
114 size_t* rowBytes) {
115 return adjust_params<void>(surfaceWidth, surfaceHeight, bpp, left, top, width, height, data,
116 rowBytes);
117}
118
119bool GrSurfacePriv::AdjustWritePixelParams(int surfaceWidth,
120 int surfaceHeight,
121 size_t bpp,
122 int* left, int* top, int* width, int* height,
123 const void** data,
124 size_t* rowBytes) {
125 return adjust_params<const void>(surfaceWidth, surfaceHeight, bpp, left, top, width, height,
126 data, rowBytes);
127}
128
129
130//////////////////////////////////////////////////////////////////////////////
131
bsalomon8d034a12014-09-22 12:21:08 -0700132bool GrSurface::hasPendingRead() const {
133 const GrTexture* thisTex = this->asTexture();
134 if (thisTex && thisTex->internalHasPendingRead()) {
135 return true;
136 }
137 const GrRenderTarget* thisRT = this->asRenderTarget();
138 if (thisRT && thisRT->internalHasPendingRead()) {
139 return true;
140 }
141 return false;
142}
143
144bool GrSurface::hasPendingWrite() const {
145 const GrTexture* thisTex = this->asTexture();
146 if (thisTex && thisTex->internalHasPendingWrite()) {
147 return true;
148 }
149 const GrRenderTarget* thisRT = this->asRenderTarget();
150 if (thisRT && thisRT->internalHasPendingWrite()) {
151 return true;
152 }
153 return false;
154}
155
156bool GrSurface::hasPendingIO() const {
157 const GrTexture* thisTex = this->asTexture();
158 if (thisTex && thisTex->internalHasPendingIO()) {
159 return true;
160 }
161 const GrRenderTarget* thisRT = this->asRenderTarget();
162 if (thisRT && thisRT->internalHasPendingIO()) {
163 return true;
164 }
165 return false;
166}
reedde499882015-06-18 13:41:40 -0700167
168void GrSurface::onRelease() {
reedde499882015-06-18 13:41:40 -0700169 this->INHERITED::onRelease();
170}
171
172void GrSurface::onAbandon() {
reedde499882015-06-18 13:41:40 -0700173 this->INHERITED::onAbandon();
174}