blob: c593f311e4d7a502a1242b824d31c1e59b47e16e [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
bsalomoneb1cb5c2015-05-22 08:01:09 -070011#include "GrCaps.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000012#include "GrContext.h"
bsalomon3582d3e2015-02-13 14:20:05 -080013#include "GrGpuResourcePriv.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000014#include "GrIndexBuffer.h"
kkinnunencabe20c2015-06-01 01:37:26 -070015#include "GrPathRendering.h"
bsalomoncb02b382015-08-12 11:14:50 -070016#include "GrPipeline.h"
bsalomon0ea80f42015-02-11 10:49:59 -080017#include "GrResourceCache.h"
egdanielec00d942015-09-14 12:56:10 -070018#include "GrResourceProvider.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080019#include "GrRenderTargetPriv.h"
egdaniel8dc7c3a2015-04-16 11:22:42 -070020#include "GrStencilAttachment.h"
egdaniel6d901da2015-07-30 12:02:15 -070021#include "GrSurfacePriv.h"
jvanverth73063dc2015-12-03 09:15:47 -080022#include "GrTransferBuffer.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000023#include "GrVertexBuffer.h"
bsalomoncb8979d2015-05-05 09:51:38 -070024#include "GrVertices.h"
cblume55f2d2d2016-02-26 13:20:48 -080025#include "SkTypes.h"
bsalomoncb8979d2015-05-05 09:51:38 -070026
27GrVertices& GrVertices::operator =(const GrVertices& di) {
28 fPrimitiveType = di.fPrimitiveType;
29 fStartVertex = di.fStartVertex;
30 fStartIndex = di.fStartIndex;
31 fVertexCount = di.fVertexCount;
32 fIndexCount = di.fIndexCount;
33
34 fInstanceCount = di.fInstanceCount;
35 fVerticesPerInstance = di.fVerticesPerInstance;
36 fIndicesPerInstance = di.fIndicesPerInstance;
bsalomone64eb572015-05-07 11:35:55 -070037 fMaxInstancesPerDraw = di.fMaxInstancesPerDraw;
bsalomoncb8979d2015-05-05 09:51:38 -070038
39 fVertexBuffer.reset(di.vertexBuffer());
40 fIndexBuffer.reset(di.indexBuffer());
41
42 return *this;
43}
bsalomon@google.com1c13c962011-02-14 16:51:21 +000044
bsalomon@google.comd302f142011-03-03 13:54:13 +000045////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000046
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000047GrGpu::GrGpu(GrContext* context)
joshualitt3322fa42014-11-07 08:48:51 -080048 : fResetTimestamp(kExpiredTimestamp+1)
bsalomon@google.com0a208a12013-06-28 18:57:35 +000049 , fResetBits(kAll_GrBackendState)
cdalton28f45b92016-03-07 13:58:26 -080050 , fMultisampleSpecsAllocator(1)
joshualitt3322fa42014-11-07 08:48:51 -080051 , fContext(context) {
reed@google.comac10a2d2010-12-22 21:39:39 +000052}
53
bsalomoned0bcad2015-05-04 10:36:42 -070054GrGpu::~GrGpu() {}
bsalomon1d89ddc2014-08-19 14:20:58 -070055
robertphillipse3371302014-09-17 06:01:06 -070056void GrGpu::contextAbandoned() {}
reed@google.comac10a2d2010-12-22 21:39:39 +000057
bsalomon@google.comd302f142011-03-03 13:54:13 +000058////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000059
bsalomon045802d2015-10-20 07:58:01 -070060bool GrGpu::makeCopyForTextureParams(int width, int height, const GrTextureParams& textureParams,
bsalomon89fe56b2015-10-29 10:49:28 -070061 GrTextureProducer::CopyParams* copyParams) const {
bsalomon045802d2015-10-20 07:58:01 -070062 const GrCaps& caps = *this->caps();
63 if (textureParams.isTiled() && !caps.npotTextureTileSupport() &&
64 (!SkIsPow2(width) || !SkIsPow2(height))) {
bsalomon100b8f82015-10-28 08:37:44 -070065 copyParams->fWidth = GrNextPow2(width);
66 copyParams->fHeight = GrNextPow2(height);
bsalomon045802d2015-10-20 07:58:01 -070067 switch (textureParams.filterMode()) {
68 case GrTextureParams::kNone_FilterMode:
69 copyParams->fFilter = GrTextureParams::kNone_FilterMode;
70 break;
71 case GrTextureParams::kBilerp_FilterMode:
72 case GrTextureParams::kMipMap_FilterMode:
73 // We are only ever scaling up so no reason to ever indicate kMipMap.
74 copyParams->fFilter = GrTextureParams::kBilerp_FilterMode;
75 break;
76 }
bsalomon100b8f82015-10-28 08:37:44 -070077 return true;
bsalomon045802d2015-10-20 07:58:01 -070078 }
bsalomon100b8f82015-10-28 08:37:44 -070079 return false;
bsalomon045802d2015-10-20 07:58:01 -070080}
81
egdanielcf614fd2015-04-22 13:58:58 -070082static GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
egdanielb0e1be22015-04-22 13:27:39 -070083 // By default, GrRenderTargets are GL's normal orientation so that they
84 // can be drawn to by the outside world without the client having
85 // to render upside down.
86 if (kDefault_GrSurfaceOrigin == origin) {
87 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
88 } else {
89 return origin;
90 }
91}
92
cblume55f2d2d2016-02-26 13:20:48 -080093/**
94 * Prior to creating a texture, make sure the type of texture being created is
95 * supported by calling check_texture_creation_params.
96 *
97 * @param caps The capabilities of the GL device.
98 * @param desc The descriptor of the texture to create.
99 * @param isRT Indicates if the texture can be a render target.
100 */
101static bool check_texture_creation_params(const GrCaps& caps, const GrSurfaceDesc& desc,
bsalomond3312592016-03-04 07:06:43 -0800102 bool* isRT) {
cblume55f2d2d2016-02-26 13:20:48 -0800103 if (!caps.isConfigTexturable(desc.fConfig)) {
104 return false;
robertphillips@google.comd3eb3362012-10-31 13:56:35 +0000105 }
krajcevski9c0e6292014-06-02 07:38:14 -0700106
cblume55f2d2d2016-02-26 13:20:48 -0800107 *isRT = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
108 if (*isRT && !caps.isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
109 return false;
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +0000110 }
robertphillips@google.comd3eb3362012-10-31 13:56:35 +0000111
robertphillips6e83ac72015-08-13 05:19:14 -0700112 // We currently do not support multisampled textures
cblume55f2d2d2016-02-26 13:20:48 -0800113 if (!*isRT && desc.fSampleCnt > 0) {
114 return false;
115 }
116
117 if (*isRT) {
118 int maxRTSize = caps.maxRenderTargetSize();
119 if (desc.fWidth > maxRTSize || desc.fHeight > maxRTSize) {
120 return false;
121 }
122 } else {
123 int maxSize = caps.maxTextureSize();
124 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
125 return false;
126 }
127 }
128 return true;
129}
130
131GrTexture* GrGpu::createTexture(const GrSurfaceDesc& origDesc, SkBudgeted budgeted,
132 const SkTArray<GrMipLevel>& texels) {
133 GrSurfaceDesc desc = origDesc;
134
135 const GrCaps* caps = this->caps();
136 bool isRT = false;
bsalomond3312592016-03-04 07:06:43 -0800137 bool textureCreationParamsValid = check_texture_creation_params(*caps, desc, &isRT);
cblume55f2d2d2016-02-26 13:20:48 -0800138 if (!textureCreationParamsValid) {
halcanary96fcdcc2015-08-27 07:41:13 -0700139 return nullptr;
egdaniel8c9b6f12015-05-12 13:36:30 -0700140 }
141
cblume55f2d2d2016-02-26 13:20:48 -0800142 desc.fSampleCnt = SkTMin(desc.fSampleCnt, caps->maxSampleCount());
143 // Attempt to catch un- or wrongly intialized sample counts;
egdanielb0e1be22015-04-22 13:27:39 -0700144 SkASSERT(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
145
146 desc.fOrigin = resolve_origin(desc.fOrigin, isRT);
147
cblume55f2d2d2016-02-26 13:20:48 -0800148 GrTexture* tex = nullptr;
149 GrGpuResource::LifeCycle lifeCycle = SkBudgeted::kYes == budgeted ?
150 GrGpuResource::kCached_LifeCycle :
151 GrGpuResource::kUncached_LifeCycle;
152
krajcevski9c0e6292014-06-02 07:38:14 -0700153 if (GrPixelConfigIsCompressed(desc.fConfig)) {
154 // We shouldn't be rendering into this
egdanielb0e1be22015-04-22 13:27:39 -0700155 SkASSERT(!isRT);
156 SkASSERT(0 == desc.fSampleCnt);
krajcevski9c0e6292014-06-02 07:38:14 -0700157
cblume55f2d2d2016-02-26 13:20:48 -0800158 if (!caps->npotTextureTileSupport() &&
tfarinaf9dae782014-06-06 06:35:28 -0700159 (!SkIsPow2(desc.fWidth) || !SkIsPow2(desc.fHeight))) {
halcanary96fcdcc2015-08-27 07:41:13 -0700160 return nullptr;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000161 }
tfarinaf9dae782014-06-06 06:35:28 -0700162
krajcevski9c0e6292014-06-02 07:38:14 -0700163 this->handleDirtyContext();
cblume55f2d2d2016-02-26 13:20:48 -0800164 tex = this->onCreateCompressedTexture(desc, lifeCycle, texels);
krajcevski9c0e6292014-06-02 07:38:14 -0700165 } else {
166 this->handleDirtyContext();
cblume55f2d2d2016-02-26 13:20:48 -0800167 tex = this->onCreateTexture(desc, lifeCycle, texels);
bsalomondb558dd2015-01-23 13:19:00 -0800168 }
bsalomonb12ea412015-02-02 21:19:50 -0800169 if (tex) {
cblume55f2d2d2016-02-26 13:20:48 -0800170 if (!caps->reuseScratchTextures() && !isRT) {
171 tex->resourcePriv().removeScratchKey();
172 }
bsalomonb12ea412015-02-02 21:19:50 -0800173 fStats.incTextureCreates();
cblume55f2d2d2016-02-26 13:20:48 -0800174 if (!texels.empty()) {
175 if (texels[0].fPixels) {
176 fStats.incTextureUploads();
177 }
bsalomonb12ea412015-02-02 21:19:50 -0800178 }
179 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000180 return tex;
181}
182
bsalomond3312592016-03-04 07:06:43 -0800183GrTexture* GrGpu::createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
184 const void* srcData, size_t rowBytes) {
185 GrMipLevel level;
186 level.fPixels = srcData;
187 level.fRowBytes = rowBytes;
188 SkSTArray<1, GrMipLevel> levels;
189 levels.push_back(level);
190
191 return this->createTexture(desc, budgeted, levels);
192}
193
bsalomon6dc6f5f2015-06-18 09:12:16 -0700194GrTexture* GrGpu::wrapBackendTexture(const GrBackendTextureDesc& desc, GrWrapOwnership ownership) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000195 this->handleDirtyContext();
bsalomon5b30c6f2015-12-17 14:17:34 -0800196 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
197 return nullptr;
198 }
199 if ((desc.fFlags & kRenderTarget_GrBackendTextureFlag) &&
200 !this->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
201 return nullptr;
202 }
ericrkf7b8b8a2016-02-24 14:49:51 -0800203 int maxSize = this->caps()->maxTextureSize();
204 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
205 return nullptr;
206 }
bsalomon6dc6f5f2015-06-18 09:12:16 -0700207 GrTexture* tex = this->onWrapBackendTexture(desc, ownership);
halcanary96fcdcc2015-08-27 07:41:13 -0700208 if (nullptr == tex) {
209 return nullptr;
bsalomon@google.coma14dd6d2012-01-03 21:08:12 +0000210 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000211 // TODO: defer this and attach dynamically
212 GrRenderTarget* tgt = tex->asRenderTarget();
egdanielec00d942015-09-14 12:56:10 -0700213 if (tgt && !fContext->resourceProvider()->attachStencilAttachment(tgt)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000214 tex->unref();
halcanary96fcdcc2015-08-27 07:41:13 -0700215 return nullptr;
bsalomon@google.come269f212011-11-07 13:29:52 +0000216 } else {
217 return tex;
218 }
219}
220
bsalomon6dc6f5f2015-06-18 09:12:16 -0700221GrRenderTarget* GrGpu::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc,
222 GrWrapOwnership ownership) {
bsalomon5b30c6f2015-12-17 14:17:34 -0800223 if (!this->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
224 return nullptr;
225 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000226 this->handleDirtyContext();
bsalomon6dc6f5f2015-06-18 09:12:16 -0700227 return this->onWrapBackendRenderTarget(desc, ownership);
bsalomon@google.come269f212011-11-07 13:29:52 +0000228}
229
ericrkf7b8b8a2016-02-24 14:49:51 -0800230GrRenderTarget* GrGpu::wrapBackendTextureAsRenderTarget(const GrBackendTextureDesc& desc,
231 GrWrapOwnership ownership) {
232 this->handleDirtyContext();
233 if (!(desc.fFlags & kRenderTarget_GrBackendTextureFlag)) {
234 return nullptr;
235 }
236 if (!this->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
237 return nullptr;
238 }
239 int maxSize = this->caps()->maxTextureSize();
240 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
241 return nullptr;
242 }
243 return this->onWrapBackendTextureAsRenderTarget(desc, ownership);
244}
245
robertphillips@google.comadacc702013-10-14 21:53:24 +0000246GrVertexBuffer* GrGpu::createVertexBuffer(size_t size, bool dynamic) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000247 this->handleDirtyContext();
robertphillips1b8e1b52015-06-24 06:54:10 -0700248 GrVertexBuffer* vb = this->onCreateVertexBuffer(size, dynamic);
249 if (!this->caps()->reuseScratchBuffers()) {
250 vb->resourcePriv().removeScratchKey();
251 }
252 return vb;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000253}
254
robertphillips@google.comadacc702013-10-14 21:53:24 +0000255GrIndexBuffer* GrGpu::createIndexBuffer(size_t size, bool dynamic) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000256 this->handleDirtyContext();
robertphillips1b8e1b52015-06-24 06:54:10 -0700257 GrIndexBuffer* ib = this->onCreateIndexBuffer(size, dynamic);
258 if (!this->caps()->reuseScratchBuffers()) {
259 ib->resourcePriv().removeScratchKey();
260 }
261 return ib;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000262}
263
jvanverth73063dc2015-12-03 09:15:47 -0800264GrTransferBuffer* GrGpu::createTransferBuffer(size_t size, TransferType type) {
265 this->handleDirtyContext();
266 GrTransferBuffer* tb = this->onCreateTransferBuffer(size, type);
267 return tb;
268}
269
egdaniel51c8d402015-08-06 10:54:13 -0700270void GrGpu::clear(const SkIRect& rect,
joshualitt3322fa42014-11-07 08:48:51 -0800271 GrColor color,
joshualitt3322fa42014-11-07 08:48:51 -0800272 GrRenderTarget* renderTarget) {
bsalomon89c62982014-11-03 12:08:42 -0800273 SkASSERT(renderTarget);
egdaniel51c8d402015-08-06 10:54:13 -0700274 SkASSERT(SkIRect::MakeWH(renderTarget->width(), renderTarget->height()).contains(rect));
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000275 this->handleDirtyContext();
egdaniel51c8d402015-08-06 10:54:13 -0700276 this->onClear(renderTarget, rect, color);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000277}
278
joshualitt6db519c2014-10-29 08:48:18 -0700279void GrGpu::clearStencilClip(const SkIRect& rect,
280 bool insideClip,
281 GrRenderTarget* renderTarget) {
joshualittd53a8272014-11-10 16:03:14 -0800282 SkASSERT(renderTarget);
joshualitt6db519c2014-10-29 08:48:18 -0700283 this->handleDirtyContext();
284 this->onClearStencilClip(renderTarget, rect, insideClip);
285}
286
joshualitt1cbdcde2015-08-21 11:53:29 -0700287bool GrGpu::copySurface(GrSurface* dst,
288 GrSurface* src,
289 const SkIRect& srcRect,
290 const SkIPoint& dstPoint) {
291 SkASSERT(dst && src);
292 this->handleDirtyContext();
293 return this->onCopySurface(dst, src, srcRect, dstPoint);
294}
295
bsalomonf0674512015-07-28 13:26:15 -0700296bool GrGpu::getReadPixelsInfo(GrSurface* srcSurface, int width, int height, size_t rowBytes,
297 GrPixelConfig readConfig, DrawPreference* drawPreference,
298 ReadPixelTempDrawInfo* tempDrawInfo) {
299 SkASSERT(drawPreference);
300 SkASSERT(tempDrawInfo);
301 SkASSERT(kGpuPrefersDraw_DrawPreference != *drawPreference);
302
egdaniel6d901da2015-07-30 12:02:15 -0700303 // We currently do not support reading into a compressed buffer
304 if (GrPixelConfigIsCompressed(readConfig)) {
305 return false;
306 }
307
bsalomonf0674512015-07-28 13:26:15 -0700308 if (!this->onGetReadPixelsInfo(srcSurface, width, height, rowBytes, readConfig, drawPreference,
309 tempDrawInfo)) {
310 return false;
311 }
312
313 // Check to see if we're going to request that the caller draw when drawing is not possible.
314 if (!srcSurface->asTexture() ||
315 !this->caps()->isConfigRenderable(tempDrawInfo->fTempSurfaceDesc.fConfig, false)) {
316 // If we don't have a fallback to a straight read then fail.
317 if (kRequireDraw_DrawPreference == *drawPreference) {
318 return false;
319 }
320 *drawPreference = kNoDraw_DrawPreference;
321 }
322
323 return true;
324}
cblumeed828002016-02-16 13:00:01 -0800325bool GrGpu::getWritePixelsInfo(GrSurface* dstSurface, int width, int height,
bsalomonf0674512015-07-28 13:26:15 -0700326 GrPixelConfig srcConfig, DrawPreference* drawPreference,
327 WritePixelTempDrawInfo* tempDrawInfo) {
328 SkASSERT(drawPreference);
329 SkASSERT(tempDrawInfo);
330 SkASSERT(kGpuPrefersDraw_DrawPreference != *drawPreference);
331
jvanverth2dc29942015-09-01 07:16:46 -0700332 if (GrPixelConfigIsCompressed(dstSurface->desc().fConfig) &&
333 dstSurface->desc().fConfig != srcConfig) {
334 return false;
335 }
336
bsalomonbabafcc2016-02-16 11:36:47 -0800337 if (SkToBool(dstSurface->asRenderTarget())) {
338 if (this->caps()->useDrawInsteadOfAllRenderTargetWrites()) {
339 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
340 } else if (this->caps()->useDrawInsteadOfPartialRenderTargetWrite() &&
341 (width < dstSurface->width() || height < dstSurface->height())) {
342 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
343 }
bsalomonf0674512015-07-28 13:26:15 -0700344 }
345
cblumeed828002016-02-16 13:00:01 -0800346 if (!this->onGetWritePixelsInfo(dstSurface, width, height, srcConfig, drawPreference,
bsalomonf0674512015-07-28 13:26:15 -0700347 tempDrawInfo)) {
348 return false;
349 }
350
351 // Check to see if we're going to request that the caller draw when drawing is not possible.
352 if (!dstSurface->asRenderTarget() ||
353 !this->caps()->isConfigTexturable(tempDrawInfo->fTempSurfaceDesc.fConfig)) {
354 // If we don't have a fallback to a straight upload then fail.
355 if (kRequireDraw_DrawPreference == *drawPreference ||
356 !this->caps()->isConfigTexturable(srcConfig)) {
357 return false;
358 }
359 *drawPreference = kNoDraw_DrawPreference;
360 }
361 return true;
362}
363
bsalomon6cb3cbe2015-07-30 07:34:27 -0700364bool GrGpu::readPixels(GrSurface* surface,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000365 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000366 GrPixelConfig config, void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000367 size_t rowBytes) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000368 this->handleDirtyContext();
egdaniel6d901da2015-07-30 12:02:15 -0700369
370 // We cannot read pixels into a compressed buffer
371 if (GrPixelConfigIsCompressed(config)) {
372 return false;
373 }
374
375 size_t bpp = GrBytesPerPixel(config);
376 if (!GrSurfacePriv::AdjustReadPixelParams(surface->width(), surface->height(), bpp,
377 &left, &top, &width, &height,
378 &buffer,
379 &rowBytes)) {
380 return false;
381 }
382
383 return this->onReadPixels(surface,
384 left, top, width, height,
385 config, buffer,
386 rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000387}
388
bsalomon6cb3cbe2015-07-30 07:34:27 -0700389bool GrGpu::writePixels(GrSurface* surface,
390 int left, int top, int width, int height,
cblume55f2d2d2016-02-26 13:20:48 -0800391 GrPixelConfig config, const SkTArray<GrMipLevel>& texels) {
392 if (!surface) {
393 return false;
394 }
bsalomond3312592016-03-04 07:06:43 -0800395 bool validMipDataFound = false;
cblume55f2d2d2016-02-26 13:20:48 -0800396 for (int currentMipLevel = 0; currentMipLevel < texels.count(); currentMipLevel++) {
bsalomond3312592016-03-04 07:06:43 -0800397 if (texels[currentMipLevel].fPixels != nullptr) {
398 validMipDataFound = true;
399 break;
cblume55f2d2d2016-02-26 13:20:48 -0800400 }
401 }
bsalomond3312592016-03-04 07:06:43 -0800402 if (!validMipDataFound) {
403 return false;
404 }
jvanverth2dc29942015-09-01 07:16:46 -0700405
bsalomon@google.com6f379512011-11-16 20:36:03 +0000406 this->handleDirtyContext();
cblume55f2d2d2016-02-26 13:20:48 -0800407 if (this->onWritePixels(surface, left, top, width, height, config, texels)) {
bsalomonb12ea412015-02-02 21:19:50 -0800408 fStats.incTextureUploads();
409 return true;
410 }
411 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000412}
413
cblume55f2d2d2016-02-26 13:20:48 -0800414bool GrGpu::writePixels(GrSurface* surface,
415 int left, int top, int width, int height,
416 GrPixelConfig config, const void* buffer,
417 size_t rowBytes) {
418 GrMipLevel mipLevel;
419 mipLevel.fPixels = buffer;
420 mipLevel.fRowBytes = rowBytes;
421 SkSTArray<1, GrMipLevel> texels;
422 texels.push_back(mipLevel);
423
424 return this->writePixels(surface, left, top, width, height, config, texels);
425}
426
jvanverth17aa0472016-01-05 10:41:27 -0800427bool GrGpu::transferPixels(GrSurface* surface,
428 int left, int top, int width, int height,
429 GrPixelConfig config, GrTransferBuffer* buffer,
430 size_t offset, size_t rowBytes) {
431 SkASSERT(buffer);
432
433 this->handleDirtyContext();
cblume61214052016-01-26 09:10:48 -0800434 if (this->onTransferPixels(surface, left, top, width, height, config,
jvanverth17aa0472016-01-05 10:41:27 -0800435 buffer, offset, rowBytes)) {
436 fStats.incTransfersToTexture();
437 return true;
438 }
439 return false;
440}
441
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000442void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000443 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000444 this->handleDirtyContext();
445 this->onResolveRenderTarget(target);
446}
447
cdalton28f45b92016-03-07 13:58:26 -0800448inline static uint8_t multisample_specs_id(uint8_t numSamples, GrSurfaceOrigin origin,
449 const GrCaps& caps) {
450 if (!caps.sampleLocationsSupport()) {
451 return numSamples;
452 }
453
454 SkASSERT(numSamples < 128);
455 SkASSERT(kTopLeft_GrSurfaceOrigin == origin || kBottomLeft_GrSurfaceOrigin == origin);
456 return (numSamples << 1) | (origin - 1);
457
458 GR_STATIC_ASSERT(1 == kTopLeft_GrSurfaceOrigin);
459 GR_STATIC_ASSERT(2 == kBottomLeft_GrSurfaceOrigin);
460}
461
462const GrGpu::MultisampleSpecs& GrGpu::getMultisampleSpecs(GrRenderTarget* rt,
463 const GrStencilSettings& stencil) {
464 const GrSurfaceDesc& desc = rt->desc();
465 uint8_t surfDescKey = multisample_specs_id(desc.fSampleCnt, desc.fOrigin, *this->caps());
466 if (fMultisampleSpecsMap.count() > surfDescKey && fMultisampleSpecsMap[surfDescKey]) {
467#if !defined(SK_DEBUG)
468 // In debug mode we query the multisample info every time and verify the caching is correct.
469 return *fMultisampleSpecsMap[surfDescKey];
470#endif
471 }
472 int effectiveSampleCnt;
473 SkAutoTDeleteArray<SkPoint> locations(nullptr);
474 this->onGetMultisampleSpecs(rt, stencil, &effectiveSampleCnt, &locations);
475 SkASSERT(effectiveSampleCnt && effectiveSampleCnt >= desc.fSampleCnt);
476 uint8_t effectiveKey = multisample_specs_id(effectiveSampleCnt, desc.fOrigin, *this->caps());
477 if (fMultisampleSpecsMap.count() > effectiveKey && fMultisampleSpecsMap[effectiveKey]) {
478 const MultisampleSpecs& specs = *fMultisampleSpecsMap[effectiveKey];
479 SkASSERT(effectiveKey == specs.fUniqueID);
480 SkASSERT(effectiveSampleCnt == specs.fEffectiveSampleCnt);
481 SkASSERT(!this->caps()->sampleLocationsSupport() ||
482 !memcmp(locations.get(), specs.fSampleLocations.get(),
483 effectiveSampleCnt * sizeof(SkPoint)));
484 SkASSERT(surfDescKey <= effectiveKey);
485 SkASSERT(!fMultisampleSpecsMap[surfDescKey] || fMultisampleSpecsMap[surfDescKey] == &specs);
486 fMultisampleSpecsMap[surfDescKey] = &specs;
487 return specs;
488 }
489 const MultisampleSpecs& specs = *new (&fMultisampleSpecsAllocator)
490 MultisampleSpecs{effectiveKey, effectiveSampleCnt, locations.detach()};
491 if (fMultisampleSpecsMap.count() <= effectiveKey) {
492 int n = 1 + effectiveKey - fMultisampleSpecsMap.count();
493 fMultisampleSpecsMap.push_back_n(n, (const MultisampleSpecs*) nullptr);
494 }
495 fMultisampleSpecsMap[effectiveKey] = &specs;
496 if (effectiveSampleCnt != desc.fSampleCnt) {
497 SkASSERT(surfDescKey < effectiveKey);
498 fMultisampleSpecsMap[surfDescKey] = &specs;
499 }
500 return specs;
501}
502
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000503////////////////////////////////////////////////////////////////////////////////
504
bsalomoncb8979d2015-05-05 09:51:38 -0700505void GrGpu::draw(const DrawArgs& args, const GrVertices& vertices) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000506 this->handleDirtyContext();
bsalomoncb02b382015-08-12 11:14:50 -0700507 if (GrXferBarrierType barrierType = args.fPipeline->xferBarrierType(*this->caps())) {
508 this->xferBarrier(args.fPipeline->getRenderTarget(), barrierType);
509 }
510
bsalomone64eb572015-05-07 11:35:55 -0700511 GrVertices::Iterator iter;
512 const GrNonInstancedVertices* verts = iter.init(vertices);
513 do {
514 this->onDraw(args, *verts);
joshualitt336cda32015-09-09 08:29:47 -0700515 fStats.incNumDraws();
bsalomone64eb572015-05-07 11:35:55 -0700516 } while ((verts = iter.next()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000517}
ethannicholas22793252016-01-30 09:59:10 -0800518