1 /*
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 "include/gpu/GrContext.h"
9 #include "include/gpu/GrSurface.h"
10 #include "include/gpu/GrTexture.h"
11 #include "src/gpu/GrOpList.h"
12 #include "src/gpu/GrRenderTarget.h"
13 #include "src/gpu/GrResourceProvider.h"
14 #include "src/gpu/GrSurfacePriv.h"
15
16 #include "src/core/SkMathPriv.h"
17 #include "src/gpu/SkGr.h"
18
WorstCaseSize(const GrSurfaceDesc & desc,GrRenderable renderable,int renderTargetSampleCnt,bool binSize)19 size_t GrSurface::WorstCaseSize(const GrSurfaceDesc& desc, GrRenderable renderable,
20 int renderTargetSampleCnt, bool binSize) {
21 size_t size;
22
23 int width = binSize ? GrResourceProvider::MakeApprox(desc.fWidth) : desc.fWidth;
24 int height = binSize ? GrResourceProvider::MakeApprox(desc.fHeight) : desc.fHeight;
25
26 if (renderable == GrRenderable::kYes) {
27 // We own one color value for each MSAA sample.
28 SkASSERT(renderTargetSampleCnt >= 1);
29 int colorValuesPerPixel = renderTargetSampleCnt;
30 if (renderTargetSampleCnt > 1) {
31 // 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);
35 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig));
36 size_t colorBytes = (size_t) width * height * GrBytesPerPixel(desc.fConfig);
37
38 // 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
44 } else {
45 SkASSERT(renderTargetSampleCnt == 1);
46 if (GrPixelConfigIsCompressed(desc.fConfig)) {
47 size = GrCompressedFormatDataSize(desc.fConfig, width, height);
48 } else {
49 size = (size_t)width * height * GrBytesPerPixel(desc.fConfig);
50 }
51
52 size += size/3; // in case we have to mipmap
53 }
54
55 return size;
56 }
57
ComputeSize(GrPixelConfig config,int width,int height,int colorSamplesPerPixel,GrMipMapped mipMapped,bool binSize)58 size_t GrSurface::ComputeSize(GrPixelConfig config,
59 int width,
60 int height,
61 int colorSamplesPerPixel,
62 GrMipMapped mipMapped,
63 bool binSize) {
64 size_t colorSize;
65
66 width = binSize ? GrResourceProvider::MakeApprox(width) : width;
67 height = binSize ? GrResourceProvider::MakeApprox(height) : height;
68
69 SkASSERT(kUnknown_GrPixelConfig != config);
70 if (GrPixelConfigIsCompressed(config)) {
71 colorSize = GrCompressedFormatDataSize(config, width, height);
72 } else {
73 colorSize = (size_t)width * height * GrBytesPerPixel(config);
74 }
75 SkASSERT(colorSize > 0);
76
77 size_t finalSize = colorSamplesPerPixel * colorSize;
78
79 if (GrMipMapped::kYes == mipMapped) {
80 // 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 }
84 return finalSize;
85 }
86
87 //////////////////////////////////////////////////////////////////////////////
88
hasPendingRead() const89 bool 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
hasPendingWrite() const101 bool 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
hasPendingIO() const113 bool 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 }
124
onRelease()125 void GrSurface::onRelease() {
126 this->invokeReleaseProc();
127 this->INHERITED::onRelease();
128 }
129
onAbandon()130 void GrSurface::onAbandon() {
131 this->invokeReleaseProc();
132 this->INHERITED::onAbandon();
133 }
134