blob: b97980f50cb27270b60b239c544b0358c4e7d7e4 [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"
bsalomonafbf2d62014-09-30 12:18:44 -070012#include "GrSurfacePriv.h"
Mike Reed84dd8572017-03-08 22:21:00 -050013#include "GrTexture.h"
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000014
Brian Osman3b655982017-03-07 16:58:08 -050015#include "SkGr.h"
Robert Phillipsb4460882016-11-17 14:43:51 -050016#include "SkMathPriv.h"
commit-bot@chromium.org6c5d9a12013-09-30 18:05:43 +000017
Robert Phillipsb4460882016-11-17 14:43:51 -050018size_t GrSurface::WorstCaseSize(const GrSurfaceDesc& desc, bool useNextPow2) {
robertphillips6e83ac72015-08-13 05:19:14 -070019 size_t size;
20
Robert Phillipsb4460882016-11-17 14:43:51 -050021 int width = useNextPow2 ? GrNextPow2(desc.fWidth) : desc.fWidth;
22 int height = useNextPow2 ? GrNextPow2(desc.fHeight) : desc.fHeight;
23
robertphillips6e83ac72015-08-13 05:19:14 -070024 bool isRenderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
25 if (isRenderTarget) {
26 // We own one color value for each MSAA sample.
27 int colorValuesPerPixel = SkTMax(1, desc.fSampleCnt);
28 if (desc.fSampleCnt) {
29 // Worse case, we own the resolve buffer so that is one more sample per pixel.
30 colorValuesPerPixel += 1;
31 }
32 SkASSERT(kUnknown_GrPixelConfig != desc.fConfig);
Robert Phillipsb4460882016-11-17 14:43:51 -050033 size_t colorBytes = (size_t) width * height * GrBytesPerPixel(desc.fConfig);
robertphillips4c56b9f2016-08-17 08:02:51 -070034
Robert Phillipsd6214d42016-11-07 08:23:48 -050035 // This would be a nice assert to have (i.e., we aren't creating 0 width/height surfaces).
36 // Unfortunately Chromium seems to want to do this.
37 //SkASSERT(colorBytes > 0);
38
39 size = colorValuesPerPixel * colorBytes;
40 size += colorBytes/3; // in case we have to mipmap
robertphillips6e83ac72015-08-13 05:19:14 -070041 } else {
Robert Phillips92de6312017-05-23 07:43:48 -040042 size = (size_t) width * height * GrBytesPerPixel(desc.fConfig);
robertphillips6e83ac72015-08-13 05:19:14 -070043
44 size += size/3; // in case we have to mipmap
45 }
46
47 return size;
48}
49
Brian Salomonbb5711a2017-05-17 13:49:59 -040050size_t GrSurface::ComputeSize(GrPixelConfig config,
51 int width,
52 int height,
Robert Phillipsd6214d42016-11-07 08:23:48 -050053 int colorSamplesPerPixel,
Robert Phillipsb4460882016-11-17 14:43:51 -050054 bool hasMIPMaps,
55 bool useNextPow2) {
Brian Salomonbb5711a2017-05-17 13:49:59 -040056 width = useNextPow2 ? GrNextPow2(width) : width;
57 height = useNextPow2 ? GrNextPow2(height) : height;
Robert Phillipsb4460882016-11-17 14:43:51 -050058
Brian Salomonbb5711a2017-05-17 13:49:59 -040059 SkASSERT(kUnknown_GrPixelConfig != config);
Robert Phillips92de6312017-05-23 07:43:48 -040060 size_t colorSize = (size_t)width * height * GrBytesPerPixel(config);
Robert Phillipsd6214d42016-11-07 08:23:48 -050061 SkASSERT(colorSize > 0);
62
63 size_t finalSize = colorSamplesPerPixel * colorSize;
64
65 if (hasMIPMaps) {
66 // We don't have to worry about the mipmaps being a different size than
67 // we'd expect because we never change fDesc.fWidth/fHeight.
68 finalSize += colorSize/3;
69 }
Robert Phillipsd6214d42016-11-07 08:23:48 -050070 return finalSize;
71}
72
bsalomone8d21e82015-07-16 08:23:13 -070073template<typename T> static bool adjust_params(int surfaceWidth,
74 int surfaceHeight,
75 size_t bpp,
76 int* left, int* top, int* width, int* height,
77 T** data,
78 size_t* rowBytes) {
79 if (!*rowBytes) {
80 *rowBytes = *width * bpp;
81 }
82
83 SkIRect subRect = SkIRect::MakeXYWH(*left, *top, *width, *height);
84 SkIRect bounds = SkIRect::MakeWH(surfaceWidth, surfaceHeight);
85
86 if (!subRect.intersect(bounds)) {
87 return false;
88 }
89 *data = reinterpret_cast<void*>(reinterpret_cast<intptr_t>(*data) +
90 (subRect.fTop - *top) * *rowBytes + (subRect.fLeft - *left) * bpp);
91
92 *left = subRect.fLeft;
93 *top = subRect.fTop;
94 *width = subRect.width();
95 *height = subRect.height();
96 return true;
97}
98
99bool GrSurfacePriv::AdjustReadPixelParams(int surfaceWidth,
100 int surfaceHeight,
101 size_t bpp,
102 int* left, int* top, int* width, int* height,
103 void** data,
104 size_t* rowBytes) {
105 return adjust_params<void>(surfaceWidth, surfaceHeight, bpp, left, top, width, height, data,
106 rowBytes);
107}
108
109bool GrSurfacePriv::AdjustWritePixelParams(int surfaceWidth,
110 int surfaceHeight,
111 size_t bpp,
112 int* left, int* top, int* width, int* height,
113 const void** data,
114 size_t* rowBytes) {
115 return adjust_params<const void>(surfaceWidth, surfaceHeight, bpp, left, top, width, height,
116 data, rowBytes);
117}
118
119
120//////////////////////////////////////////////////////////////////////////////
121
bsalomon8d034a12014-09-22 12:21:08 -0700122bool GrSurface::hasPendingRead() const {
123 const GrTexture* thisTex = this->asTexture();
124 if (thisTex && thisTex->internalHasPendingRead()) {
125 return true;
126 }
127 const GrRenderTarget* thisRT = this->asRenderTarget();
128 if (thisRT && thisRT->internalHasPendingRead()) {
129 return true;
130 }
131 return false;
132}
133
134bool GrSurface::hasPendingWrite() const {
135 const GrTexture* thisTex = this->asTexture();
136 if (thisTex && thisTex->internalHasPendingWrite()) {
137 return true;
138 }
139 const GrRenderTarget* thisRT = this->asRenderTarget();
140 if (thisRT && thisRT->internalHasPendingWrite()) {
141 return true;
142 }
143 return false;
144}
145
146bool GrSurface::hasPendingIO() const {
147 const GrTexture* thisTex = this->asTexture();
148 if (thisTex && thisTex->internalHasPendingIO()) {
149 return true;
150 }
151 const GrRenderTarget* thisRT = this->asRenderTarget();
152 if (thisRT && thisRT->internalHasPendingIO()) {
153 return true;
154 }
155 return false;
156}
reedde499882015-06-18 13:41:40 -0700157
158void GrSurface::onRelease() {
reedde499882015-06-18 13:41:40 -0700159 this->INHERITED::onRelease();
160}
161
162void GrSurface::onAbandon() {
reedde499882015-06-18 13:41:40 -0700163 this->INHERITED::onAbandon();
164}