blob: 691c0ab83abbaf672b6c031dcf12bad0bfbdeda6 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2010 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.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
reed@google.comac10a2d2010-12-22 21:39:39 +00009#include "GrGpu.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000010
cdalton397536c2016-03-25 12:15:03 -070011#include "GrBuffer.h"
bsalomoneb1cb5c2015-05-22 08:01:09 -070012#include "GrCaps.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000013#include "GrContext.h"
bsalomon3582d3e2015-02-13 14:20:05 -080014#include "GrGpuResourcePriv.h"
egdaniel0e1853c2016-03-17 11:35:45 -070015#include "GrMesh.h"
kkinnunencabe20c2015-06-01 01:37:26 -070016#include "GrPathRendering.h"
bsalomoncb02b382015-08-12 11:14:50 -070017#include "GrPipeline.h"
bsalomon0ea80f42015-02-11 10:49:59 -080018#include "GrResourceCache.h"
egdanielec00d942015-09-14 12:56:10 -070019#include "GrResourceProvider.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080020#include "GrRenderTargetPriv.h"
egdaniel8dc7c3a2015-04-16 11:22:42 -070021#include "GrStencilAttachment.h"
egdaniel6d901da2015-07-30 12:02:15 -070022#include "GrSurfacePriv.h"
jvanverth900bd4a2016-04-29 13:53:12 -070023#include "GrTexturePriv.h"
cblume55f2d2d2016-02-26 13:20:48 -080024#include "SkTypes.h"
bsalomoncb8979d2015-05-05 09:51:38 -070025
egdaniel0e1853c2016-03-17 11:35:45 -070026GrMesh& GrMesh::operator =(const GrMesh& di) {
bsalomoncb8979d2015-05-05 09:51:38 -070027 fPrimitiveType = di.fPrimitiveType;
28 fStartVertex = di.fStartVertex;
29 fStartIndex = di.fStartIndex;
30 fVertexCount = di.fVertexCount;
31 fIndexCount = di.fIndexCount;
32
33 fInstanceCount = di.fInstanceCount;
34 fVerticesPerInstance = di.fVerticesPerInstance;
35 fIndicesPerInstance = di.fIndicesPerInstance;
bsalomone64eb572015-05-07 11:35:55 -070036 fMaxInstancesPerDraw = di.fMaxInstancesPerDraw;
bsalomoncb8979d2015-05-05 09:51:38 -070037
38 fVertexBuffer.reset(di.vertexBuffer());
39 fIndexBuffer.reset(di.indexBuffer());
40
41 return *this;
42}
bsalomon@google.com1c13c962011-02-14 16:51:21 +000043
bsalomon@google.comd302f142011-03-03 13:54:13 +000044////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000045
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000046GrGpu::GrGpu(GrContext* context)
joshualitt3322fa42014-11-07 08:48:51 -080047 : fResetTimestamp(kExpiredTimestamp+1)
bsalomon@google.com0a208a12013-06-28 18:57:35 +000048 , fResetBits(kAll_GrBackendState)
cdalton28f45b92016-03-07 13:58:26 -080049 , fMultisampleSpecsAllocator(1)
joshualitt3322fa42014-11-07 08:48:51 -080050 , fContext(context) {
reed@google.comac10a2d2010-12-22 21:39:39 +000051}
52
bsalomoned0bcad2015-05-04 10:36:42 -070053GrGpu::~GrGpu() {}
bsalomon1d89ddc2014-08-19 14:20:58 -070054
bsalomon6e2aad42016-04-01 11:54:31 -070055void GrGpu::disconnect(DisconnectType) {}
reed@google.comac10a2d2010-12-22 21:39:39 +000056
bsalomon@google.comd302f142011-03-03 13:54:13 +000057////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000058
bsalomon045802d2015-10-20 07:58:01 -070059bool GrGpu::makeCopyForTextureParams(int width, int height, const GrTextureParams& textureParams,
bsalomon89fe56b2015-10-29 10:49:28 -070060 GrTextureProducer::CopyParams* copyParams) const {
bsalomon045802d2015-10-20 07:58:01 -070061 const GrCaps& caps = *this->caps();
62 if (textureParams.isTiled() && !caps.npotTextureTileSupport() &&
63 (!SkIsPow2(width) || !SkIsPow2(height))) {
bsalomon100b8f82015-10-28 08:37:44 -070064 copyParams->fWidth = GrNextPow2(width);
65 copyParams->fHeight = GrNextPow2(height);
bsalomon045802d2015-10-20 07:58:01 -070066 switch (textureParams.filterMode()) {
67 case GrTextureParams::kNone_FilterMode:
68 copyParams->fFilter = GrTextureParams::kNone_FilterMode;
69 break;
70 case GrTextureParams::kBilerp_FilterMode:
71 case GrTextureParams::kMipMap_FilterMode:
72 // We are only ever scaling up so no reason to ever indicate kMipMap.
73 copyParams->fFilter = GrTextureParams::kBilerp_FilterMode;
74 break;
75 }
bsalomon100b8f82015-10-28 08:37:44 -070076 return true;
bsalomon045802d2015-10-20 07:58:01 -070077 }
bsalomon100b8f82015-10-28 08:37:44 -070078 return false;
bsalomon045802d2015-10-20 07:58:01 -070079}
80
egdanielcf614fd2015-04-22 13:58:58 -070081static GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
egdanielb0e1be22015-04-22 13:27:39 -070082 // By default, GrRenderTargets are GL's normal orientation so that they
83 // can be drawn to by the outside world without the client having
84 // to render upside down.
85 if (kDefault_GrSurfaceOrigin == origin) {
86 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
87 } else {
88 return origin;
89 }
90}
91
cblume55f2d2d2016-02-26 13:20:48 -080092/**
93 * Prior to creating a texture, make sure the type of texture being created is
94 * supported by calling check_texture_creation_params.
95 *
96 * @param caps The capabilities of the GL device.
97 * @param desc The descriptor of the texture to create.
98 * @param isRT Indicates if the texture can be a render target.
99 */
100static bool check_texture_creation_params(const GrCaps& caps, const GrSurfaceDesc& desc,
bsalomone699d0c2016-03-09 06:25:15 -0800101 bool* isRT, const SkTArray<GrMipLevel>& texels) {
cblume55f2d2d2016-02-26 13:20:48 -0800102 if (!caps.isConfigTexturable(desc.fConfig)) {
103 return false;
robertphillips@google.comd3eb3362012-10-31 13:56:35 +0000104 }
krajcevski9c0e6292014-06-02 07:38:14 -0700105
cblume55f2d2d2016-02-26 13:20:48 -0800106 *isRT = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
107 if (*isRT && !caps.isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
108 return false;
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +0000109 }
robertphillips@google.comd3eb3362012-10-31 13:56:35 +0000110
robertphillips6e83ac72015-08-13 05:19:14 -0700111 // We currently do not support multisampled textures
cblume55f2d2d2016-02-26 13:20:48 -0800112 if (!*isRT && desc.fSampleCnt > 0) {
113 return false;
114 }
115
116 if (*isRT) {
117 int maxRTSize = caps.maxRenderTargetSize();
118 if (desc.fWidth > maxRTSize || desc.fHeight > maxRTSize) {
119 return false;
120 }
121 } else {
122 int maxSize = caps.maxTextureSize();
123 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
124 return false;
125 }
126 }
bsalomone699d0c2016-03-09 06:25:15 -0800127
128 for (int i = 0; i < texels.count(); ++i) {
129 if (!texels[i].fPixels) {
130 return false;
131 }
132 }
cblume55f2d2d2016-02-26 13:20:48 -0800133 return true;
134}
135
136GrTexture* GrGpu::createTexture(const GrSurfaceDesc& origDesc, SkBudgeted budgeted,
137 const SkTArray<GrMipLevel>& texels) {
138 GrSurfaceDesc desc = origDesc;
139
140 const GrCaps* caps = this->caps();
141 bool isRT = false;
bsalomone699d0c2016-03-09 06:25:15 -0800142 bool textureCreationParamsValid = check_texture_creation_params(*caps, desc, &isRT, texels);
cblume55f2d2d2016-02-26 13:20:48 -0800143 if (!textureCreationParamsValid) {
halcanary96fcdcc2015-08-27 07:41:13 -0700144 return nullptr;
egdaniel8c9b6f12015-05-12 13:36:30 -0700145 }
146
cblume55f2d2d2016-02-26 13:20:48 -0800147 desc.fSampleCnt = SkTMin(desc.fSampleCnt, caps->maxSampleCount());
148 // Attempt to catch un- or wrongly intialized sample counts;
egdanielb0e1be22015-04-22 13:27:39 -0700149 SkASSERT(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
150
151 desc.fOrigin = resolve_origin(desc.fOrigin, isRT);
152
cblume55f2d2d2016-02-26 13:20:48 -0800153 GrTexture* tex = nullptr;
cblume55f2d2d2016-02-26 13:20:48 -0800154
krajcevski9c0e6292014-06-02 07:38:14 -0700155 if (GrPixelConfigIsCompressed(desc.fConfig)) {
156 // We shouldn't be rendering into this
egdanielb0e1be22015-04-22 13:27:39 -0700157 SkASSERT(!isRT);
158 SkASSERT(0 == desc.fSampleCnt);
krajcevski9c0e6292014-06-02 07:38:14 -0700159
cblume55f2d2d2016-02-26 13:20:48 -0800160 if (!caps->npotTextureTileSupport() &&
tfarinaf9dae782014-06-06 06:35:28 -0700161 (!SkIsPow2(desc.fWidth) || !SkIsPow2(desc.fHeight))) {
halcanary96fcdcc2015-08-27 07:41:13 -0700162 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000163 }
tfarinaf9dae782014-06-06 06:35:28 -0700164
krajcevski9c0e6292014-06-02 07:38:14 -0700165 this->handleDirtyContext();
kkinnunen2e6055b2016-04-22 01:48:29 -0700166 tex = this->onCreateCompressedTexture(desc, budgeted, texels);
krajcevski9c0e6292014-06-02 07:38:14 -0700167 } else {
168 this->handleDirtyContext();
kkinnunen2e6055b2016-04-22 01:48:29 -0700169 tex = this->onCreateTexture(desc, budgeted, texels);
bsalomondb558dd2015-01-23 13:19:00 -0800170 }
bsalomonb12ea412015-02-02 21:19:50 -0800171 if (tex) {
cblume55f2d2d2016-02-26 13:20:48 -0800172 if (!caps->reuseScratchTextures() && !isRT) {
173 tex->resourcePriv().removeScratchKey();
174 }
bsalomonb12ea412015-02-02 21:19:50 -0800175 fStats.incTextureCreates();
cblume55f2d2d2016-02-26 13:20:48 -0800176 if (!texels.empty()) {
177 if (texels[0].fPixels) {
178 fStats.incTextureUploads();
179 }
bsalomonb12ea412015-02-02 21:19:50 -0800180 }
181 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000182 return tex;
183}
184
bsalomon6dc6f5f2015-06-18 09:12:16 -0700185GrTexture* GrGpu::wrapBackendTexture(const GrBackendTextureDesc& desc, GrWrapOwnership ownership) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000186 this->handleDirtyContext();
bsalomon5b30c6f2015-12-17 14:17:34 -0800187 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
188 return nullptr;
189 }
190 if ((desc.fFlags & kRenderTarget_GrBackendTextureFlag) &&
191 !this->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
192 return nullptr;
193 }
ericrkf7b8b8a2016-02-24 14:49:51 -0800194 int maxSize = this->caps()->maxTextureSize();
195 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
196 return nullptr;
197 }
bsalomon6dc6f5f2015-06-18 09:12:16 -0700198 GrTexture* tex = this->onWrapBackendTexture(desc, ownership);
halcanary96fcdcc2015-08-27 07:41:13 -0700199 if (nullptr == tex) {
200 return nullptr;
bsalomon@google.coma14dd6d2012-01-03 21:08:12 +0000201 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000202 // TODO: defer this and attach dynamically
203 GrRenderTarget* tgt = tex->asRenderTarget();
egdanielec00d942015-09-14 12:56:10 -0700204 if (tgt && !fContext->resourceProvider()->attachStencilAttachment(tgt)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000205 tex->unref();
halcanary96fcdcc2015-08-27 07:41:13 -0700206 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000207 } else {
208 return tex;
209 }
210}
211
bsalomon6dc6f5f2015-06-18 09:12:16 -0700212GrRenderTarget* GrGpu::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc,
213 GrWrapOwnership ownership) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800214 if (!this->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
215 return nullptr;
216 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000217 this->handleDirtyContext();
bsalomon6dc6f5f2015-06-18 09:12:16 -0700218 return this->onWrapBackendRenderTarget(desc, ownership);
bsalomon@google.come269f212011-11-07 13:29:52 +0000219}
220
kkinnunen49c4c222016-04-01 04:50:37 -0700221GrRenderTarget* GrGpu::wrapBackendTextureAsRenderTarget(const GrBackendTextureDesc& desc) {
ericrkf7b8b8a2016-02-24 14:49:51 -0800222 this->handleDirtyContext();
223 if (!(desc.fFlags & kRenderTarget_GrBackendTextureFlag)) {
224 return nullptr;
225 }
226 if (!this->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
227 return nullptr;
228 }
229 int maxSize = this->caps()->maxTextureSize();
230 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
231 return nullptr;
232 }
kkinnunen49c4c222016-04-01 04:50:37 -0700233 return this->onWrapBackendTextureAsRenderTarget(desc);
ericrkf7b8b8a2016-02-24 14:49:51 -0800234}
235
cdaltone2e71c22016-04-07 18:13:29 -0700236GrBuffer* GrGpu::createBuffer(size_t size, GrBufferType intendedType,
cdalton1bf3e712016-04-19 10:00:02 -0700237 GrAccessPattern accessPattern, const void* data) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000238 this->handleDirtyContext();
cdalton1bf3e712016-04-19 10:00:02 -0700239 GrBuffer* buffer = this->onCreateBuffer(size, intendedType, accessPattern, data);
robertphillips1b8e1b52015-06-24 06:54:10 -0700240 if (!this->caps()->reuseScratchBuffers()) {
cdalton397536c2016-03-25 12:15:03 -0700241 buffer->resourcePriv().removeScratchKey();
robertphillips1b8e1b52015-06-24 06:54:10 -0700242 }
cdalton397536c2016-03-25 12:15:03 -0700243 return buffer;
jvanverth73063dc2015-12-03 09:15:47 -0800244}
245
egdaniel51c8d402015-08-06 10:54:13 -0700246void GrGpu::clear(const SkIRect& rect,
joshualitt3322fa42014-11-07 08:48:51 -0800247 GrColor color,
joshualitt3322fa42014-11-07 08:48:51 -0800248 GrRenderTarget* renderTarget) {
bsalomon89c62982014-11-03 12:08:42 -0800249 SkASSERT(renderTarget);
egdaniel51c8d402015-08-06 10:54:13 -0700250 SkASSERT(SkIRect::MakeWH(renderTarget->width(), renderTarget->height()).contains(rect));
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000251 this->handleDirtyContext();
egdaniel51c8d402015-08-06 10:54:13 -0700252 this->onClear(renderTarget, rect, color);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000253}
254
joshualitt6db519c2014-10-29 08:48:18 -0700255void GrGpu::clearStencilClip(const SkIRect& rect,
256 bool insideClip,
257 GrRenderTarget* renderTarget) {
joshualittd53a8272014-11-10 16:03:14 -0800258 SkASSERT(renderTarget);
joshualitt6db519c2014-10-29 08:48:18 -0700259 this->handleDirtyContext();
260 this->onClearStencilClip(renderTarget, rect, insideClip);
261}
262
joshualitt1cbdcde2015-08-21 11:53:29 -0700263bool GrGpu::copySurface(GrSurface* dst,
264 GrSurface* src,
265 const SkIRect& srcRect,
266 const SkIPoint& dstPoint) {
267 SkASSERT(dst && src);
268 this->handleDirtyContext();
269 return this->onCopySurface(dst, src, srcRect, dstPoint);
270}
271
bsalomonf0674512015-07-28 13:26:15 -0700272bool GrGpu::getReadPixelsInfo(GrSurface* srcSurface, int width, int height, size_t rowBytes,
273 GrPixelConfig readConfig, DrawPreference* drawPreference,
274 ReadPixelTempDrawInfo* tempDrawInfo) {
275 SkASSERT(drawPreference);
276 SkASSERT(tempDrawInfo);
277 SkASSERT(kGpuPrefersDraw_DrawPreference != *drawPreference);
278
egdaniel6d901da2015-07-30 12:02:15 -0700279 // We currently do not support reading into a compressed buffer
280 if (GrPixelConfigIsCompressed(readConfig)) {
281 return false;
282 }
283
bsalomonf0674512015-07-28 13:26:15 -0700284 if (!this->onGetReadPixelsInfo(srcSurface, width, height, rowBytes, readConfig, drawPreference,
285 tempDrawInfo)) {
286 return false;
287 }
288
289 // Check to see if we're going to request that the caller draw when drawing is not possible.
290 if (!srcSurface->asTexture() ||
291 !this->caps()->isConfigRenderable(tempDrawInfo->fTempSurfaceDesc.fConfig, false)) {
292 // If we don't have a fallback to a straight read then fail.
293 if (kRequireDraw_DrawPreference == *drawPreference) {
294 return false;
295 }
296 *drawPreference = kNoDraw_DrawPreference;
297 }
298
299 return true;
300}
cblumeed828002016-02-16 13:00:01 -0800301bool GrGpu::getWritePixelsInfo(GrSurface* dstSurface, int width, int height,
bsalomonf0674512015-07-28 13:26:15 -0700302 GrPixelConfig srcConfig, DrawPreference* drawPreference,
303 WritePixelTempDrawInfo* tempDrawInfo) {
304 SkASSERT(drawPreference);
305 SkASSERT(tempDrawInfo);
306 SkASSERT(kGpuPrefersDraw_DrawPreference != *drawPreference);
307
jvanverth2dc29942015-09-01 07:16:46 -0700308 if (GrPixelConfigIsCompressed(dstSurface->desc().fConfig) &&
309 dstSurface->desc().fConfig != srcConfig) {
310 return false;
311 }
312
bsalomonbabafcc2016-02-16 11:36:47 -0800313 if (SkToBool(dstSurface->asRenderTarget())) {
314 if (this->caps()->useDrawInsteadOfAllRenderTargetWrites()) {
315 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
316 } else if (this->caps()->useDrawInsteadOfPartialRenderTargetWrite() &&
317 (width < dstSurface->width() || height < dstSurface->height())) {
318 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
319 }
bsalomonf0674512015-07-28 13:26:15 -0700320 }
321
cblumeed828002016-02-16 13:00:01 -0800322 if (!this->onGetWritePixelsInfo(dstSurface, width, height, srcConfig, drawPreference,
bsalomonf0674512015-07-28 13:26:15 -0700323 tempDrawInfo)) {
324 return false;
325 }
326
327 // Check to see if we're going to request that the caller draw when drawing is not possible.
328 if (!dstSurface->asRenderTarget() ||
329 !this->caps()->isConfigTexturable(tempDrawInfo->fTempSurfaceDesc.fConfig)) {
330 // If we don't have a fallback to a straight upload then fail.
331 if (kRequireDraw_DrawPreference == *drawPreference ||
332 !this->caps()->isConfigTexturable(srcConfig)) {
333 return false;
334 }
335 *drawPreference = kNoDraw_DrawPreference;
336 }
337 return true;
338}
339
bsalomon6cb3cbe2015-07-30 07:34:27 -0700340bool GrGpu::readPixels(GrSurface* surface,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000341 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000342 GrPixelConfig config, void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000343 size_t rowBytes) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000344 this->handleDirtyContext();
egdaniel6d901da2015-07-30 12:02:15 -0700345
346 // We cannot read pixels into a compressed buffer
347 if (GrPixelConfigIsCompressed(config)) {
348 return false;
349 }
350
351 size_t bpp = GrBytesPerPixel(config);
352 if (!GrSurfacePriv::AdjustReadPixelParams(surface->width(), surface->height(), bpp,
353 &left, &top, &width, &height,
354 &buffer,
355 &rowBytes)) {
356 return false;
357 }
358
359 return this->onReadPixels(surface,
360 left, top, width, height,
361 config, buffer,
362 rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000363}
364
bsalomon6cb3cbe2015-07-30 07:34:27 -0700365bool GrGpu::writePixels(GrSurface* surface,
366 int left, int top, int width, int height,
cblume55f2d2d2016-02-26 13:20:48 -0800367 GrPixelConfig config, const SkTArray<GrMipLevel>& texels) {
368 if (!surface) {
369 return false;
370 }
cblume55f2d2d2016-02-26 13:20:48 -0800371 for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLevel++) {
bsalomone699d0c2016-03-09 06:25:15 -0800372 if (!texels[currentMipLevel].fPixels ) {
373 return false;
cblume55f2d2d2016-02-26 13:20:48 -0800374 }
375 }
jvanverth2dc29942015-09-01 07:16:46 -0700376
bsalomon@google.com6f379512011-11-16 20:36:03 +0000377 this->handleDirtyContext();
cblume55f2d2d2016-02-26 13:20:48 -0800378 if (this->onWritePixels(surface, left, top, width, height, config, texels)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700379 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
380 this->didWriteToSurface(surface, &rect, texels.count());
bsalomonb12ea412015-02-02 21:19:50 -0800381 fStats.incTextureUploads();
382 return true;
383 }
384 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000385}
386
cblume55f2d2d2016-02-26 13:20:48 -0800387bool GrGpu::writePixels(GrSurface* surface,
388 int left, int top, int width, int height,
389 GrPixelConfig config, const void* buffer,
390 size_t rowBytes) {
391 GrMipLevel mipLevel;
392 mipLevel.fPixels = buffer;
393 mipLevel.fRowBytes = rowBytes;
394 SkSTArray<1, GrMipLevel> texels;
395 texels.push_back(mipLevel);
396
397 return this->writePixels(surface, left, top, width, height, config, texels);
398}
399
jvanverthc3d706f2016-04-20 10:33:27 -0700400bool GrGpu::transferPixels(GrSurface* surface,
jvanverth17aa0472016-01-05 10:41:27 -0800401 int left, int top, int width, int height,
cdalton397536c2016-03-25 12:15:03 -0700402 GrPixelConfig config, GrBuffer* transferBuffer,
jvanverth17aa0472016-01-05 10:41:27 -0800403 size_t offset, size_t rowBytes) {
cdalton397536c2016-03-25 12:15:03 -0700404 SkASSERT(transferBuffer);
jvanverth17aa0472016-01-05 10:41:27 -0800405
406 this->handleDirtyContext();
jvanverthc3d706f2016-04-20 10:33:27 -0700407 if (this->onTransferPixels(surface, left, top, width, height, config,
cdalton397536c2016-03-25 12:15:03 -0700408 transferBuffer, offset, rowBytes)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700409 SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
410 this->didWriteToSurface(surface, &rect);
jvanverth17aa0472016-01-05 10:41:27 -0800411 fStats.incTransfersToTexture();
412 return true;
413 }
414 return false;
415}
416
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000417void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000418 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000419 this->handleDirtyContext();
420 this->onResolveRenderTarget(target);
421}
422
jvanverth900bd4a2016-04-29 13:53:12 -0700423void GrGpu::didWriteToSurface(GrSurface* surface, const SkIRect* bounds, uint32_t mipLevels) const {
424 SkASSERT(surface);
425 // Mark any MIP chain and resolve buffer as dirty if and only if there is a non-empty bounds.
426 if (nullptr == bounds || !bounds->isEmpty()) {
427 if (GrRenderTarget* target = surface->asRenderTarget()) {
428 target->flagAsNeedingResolve(bounds);
429 }
430 GrTexture* texture = surface->asTexture();
431 if (texture && 1 == mipLevels) {
432 texture->texturePriv().dirtyMipMaps(true);
433 }
434 }
435}
436
cdalton28f45b92016-03-07 13:58:26 -0800437inline static uint8_t multisample_specs_id(uint8_t numSamples, GrSurfaceOrigin origin,
438 const GrCaps& caps) {
439 if (!caps.sampleLocationsSupport()) {
440 return numSamples;
441 }
442
443 SkASSERT(numSamples < 128);
444 SkASSERT(kTopLeft_GrSurfaceOrigin == origin || kBottomLeft_GrSurfaceOrigin == origin);
445 return (numSamples << 1) | (origin - 1);
446
447 GR_STATIC_ASSERT(1 == kTopLeft_GrSurfaceOrigin);
448 GR_STATIC_ASSERT(2 == kBottomLeft_GrSurfaceOrigin);
449}
450
451const GrGpu::MultisampleSpecs& GrGpu::getMultisampleSpecs(GrRenderTarget* rt,
452 const GrStencilSettings& stencil) {
453 const GrSurfaceDesc& desc = rt->desc();
454 uint8_t surfDescKey = multisample_specs_id(desc.fSampleCnt, desc.fOrigin, *this->caps());
455 if (fMultisampleSpecsMap.count() > surfDescKey && fMultisampleSpecsMap[surfDescKey]) {
456#if !defined(SK_DEBUG)
457 // In debug mode we query the multisample info every time and verify the caching is correct.
458 return *fMultisampleSpecsMap[surfDescKey];
459#endif
460 }
461 int effectiveSampleCnt;
462 SkAutoTDeleteArray<SkPoint> locations(nullptr);
463 this->onGetMultisampleSpecs(rt, stencil, &effectiveSampleCnt, &locations);
464 SkASSERT(effectiveSampleCnt && effectiveSampleCnt >= desc.fSampleCnt);
465 uint8_t effectiveKey = multisample_specs_id(effectiveSampleCnt, desc.fOrigin, *this->caps());
466 if (fMultisampleSpecsMap.count() > effectiveKey && fMultisampleSpecsMap[effectiveKey]) {
467 const MultisampleSpecs& specs = *fMultisampleSpecsMap[effectiveKey];
468 SkASSERT(effectiveKey == specs.fUniqueID);
469 SkASSERT(effectiveSampleCnt == specs.fEffectiveSampleCnt);
470 SkASSERT(!this->caps()->sampleLocationsSupport() ||
471 !memcmp(locations.get(), specs.fSampleLocations.get(),
472 effectiveSampleCnt * sizeof(SkPoint)));
473 SkASSERT(surfDescKey <= effectiveKey);
474 SkASSERT(!fMultisampleSpecsMap[surfDescKey] || fMultisampleSpecsMap[surfDescKey] == &specs);
475 fMultisampleSpecsMap[surfDescKey] = &specs;
476 return specs;
477 }
478 const MultisampleSpecs& specs = *new (&fMultisampleSpecsAllocator)
mtklein18300a32016-03-16 13:53:35 -0700479 MultisampleSpecs{effectiveKey, effectiveSampleCnt, locations.release()};
cdalton28f45b92016-03-07 13:58:26 -0800480 if (fMultisampleSpecsMap.count() <= effectiveKey) {
481 int n = 1 + effectiveKey - fMultisampleSpecsMap.count();
482 fMultisampleSpecsMap.push_back_n(n, (const MultisampleSpecs*) nullptr);
483 }
484 fMultisampleSpecsMap[effectiveKey] = &specs;
485 if (effectiveSampleCnt != desc.fSampleCnt) {
486 SkASSERT(surfDescKey < effectiveKey);
487 fMultisampleSpecsMap[surfDescKey] = &specs;
488 }
489 return specs;
490}
491
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000492////////////////////////////////////////////////////////////////////////////////
493
bsalomon7dbd45d2016-03-23 10:40:53 -0700494bool GrGpu::draw(const GrPipeline& pipeline,
egdaniel0e1853c2016-03-17 11:35:45 -0700495 const GrPrimitiveProcessor& primProc,
496 const GrMesh* meshes,
497 int meshCount) {
bsalomon7dbd45d2016-03-23 10:40:53 -0700498 if (primProc.numAttribs() > this->caps()->maxVertexAttributes()) {
bsalomon1d417a82016-03-23 11:50:26 -0700499 fStats.incNumFailedDraws();
bsalomon7dbd45d2016-03-23 10:40:53 -0700500 return false;
501 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000502 this->handleDirtyContext();
bsalomoncb02b382015-08-12 11:14:50 -0700503
egdaniel0e1853c2016-03-17 11:35:45 -0700504 this->onDraw(pipeline, primProc, meshes, meshCount);
bsalomon7dbd45d2016-03-23 10:40:53 -0700505 return true;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000506}