blob: ac19e91c476916c1c00136ac967e0f8c72a4c16c [file] [log] [blame]
Brian Osman45580d32016-11-23 09:37:01 -05001/*
2 * Copyright 2016 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
Greg Daniel46cfbc62019-06-07 11:43:30 -04008#include "src/gpu/GrSurfaceContext.h"
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/private/GrAuditTrail.h"
11#include "include/private/GrOpList.h"
12#include "include/private/GrRecordingContext.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040013#include "src/core/SkAutoPixmapStorage.h"
14#include "src/gpu/GrClip.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrContextPriv.h"
16#include "src/gpu/GrDrawingManager.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040017#include "src/gpu/GrGpu.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrRecordingContextPriv.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040019#include "src/gpu/GrRenderTargetContext.h"
Greg Daniel46cfbc62019-06-07 11:43:30 -040020#include "src/gpu/GrSurfaceContextPriv.h"
Greg Daniel6eb8c242019-06-05 10:22:24 -040021#include "src/gpu/GrSurfacePriv.h"
22#include "src/gpu/GrTextureContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/SkGr.h"
Brian Osman45580d32016-11-23 09:37:01 -050024
Robert Phillips2de8cfa2017-06-28 10:33:41 -040025#define ASSERT_SINGLE_OWNER \
26 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
Robert Phillips69893702019-02-22 11:16:30 -050027#define RETURN_FALSE_IF_ABANDONED if (this->fContext->priv().abandoned()) { return false; }
Brian Osman45580d32016-11-23 09:37:01 -050028
29// In MDB mode the reffing of the 'getLastOpList' call's result allows in-progress
30// GrOpLists to be picked up and added to by renderTargetContexts lower in the call
31// stack. When this occurs with a closed GrOpList, a new one will be allocated
32// when the renderTargetContext attempts to use it (via getOpList).
Robert Phillips69893702019-02-22 11:16:30 -050033GrSurfaceContext::GrSurfaceContext(GrRecordingContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -040034 GrPixelConfig config,
Robert Phillips0d075de2019-03-04 11:08:13 -050035 sk_sp<SkColorSpace> colorSpace)
Brian Salomonf3569f02017-10-24 12:52:33 -040036 : fContext(context)
Robert Phillips0d075de2019-03-04 11:08:13 -050037 , fColorSpaceInfo(std::move(colorSpace), config) {
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040038}
39
Robert Phillips0d075de2019-03-04 11:08:13 -050040GrAuditTrail* GrSurfaceContext::auditTrail() {
41 return fContext->priv().auditTrail();
42}
43
44GrDrawingManager* GrSurfaceContext::drawingManager() {
45 return fContext->priv().drawingManager();
46}
47
48const GrDrawingManager* GrSurfaceContext::drawingManager() const {
49 return fContext->priv().drawingManager();
50}
51
52#ifdef SK_DEBUG
53GrSingleOwner* GrSurfaceContext::singleOwner() {
54 return fContext->priv().singleOwner();
55}
56#endif
Greg Daniel6eb8c242019-06-05 10:22:24 -040057static bool valid_premul_color_type(GrColorType ct) {
58 switch (ct) {
59 case GrColorType::kUnknown: return false;
60 case GrColorType::kAlpha_8: return false;
61 case GrColorType::kRGB_565: return false;
62 case GrColorType::kABGR_4444: return true;
63 case GrColorType::kRGBA_8888: return true;
64 case GrColorType::kRGB_888x: return false;
65 case GrColorType::kRG_88: return false;
66 case GrColorType::kBGRA_8888: return true;
67 case GrColorType::kRGBA_1010102: return true;
68 case GrColorType::kGray_8: return false;
69 case GrColorType::kAlpha_F16: return false;
70 case GrColorType::kRGBA_F16: return true;
71 case GrColorType::kRGBA_F16_Clamped: return true;
72 case GrColorType::kRG_F32: return false;
73 case GrColorType::kRGBA_F32: return true;
74 case GrColorType::kRGB_ETC1: return false;
Robert Phillipsfe18de52019-06-06 17:21:50 -040075 // Experimental (for P016 and P010)
76 case GrColorType::kR_16: return false;
77 case GrColorType::kRG_1616: return false;
Greg Daniel6eb8c242019-06-05 10:22:24 -040078 }
79 SK_ABORT("Invalid GrColorType");
80 return false;
81}
82
83// TODO: This will be removed when GrSurfaceContexts are aware of their color types.
84// (skbug.com/6718)
85static bool valid_premul_config(GrPixelConfig config) {
86 switch (config) {
87 case kUnknown_GrPixelConfig: return false;
88 case kAlpha_8_GrPixelConfig: return false;
89 case kGray_8_GrPixelConfig: return false;
90 case kRGB_565_GrPixelConfig: return false;
91 case kRGBA_4444_GrPixelConfig: return true;
92 case kRGBA_8888_GrPixelConfig: return true;
93 case kRGB_888_GrPixelConfig: return false;
94 case kRGB_888X_GrPixelConfig: return false;
95 case kRG_88_GrPixelConfig: return false;
96 case kBGRA_8888_GrPixelConfig: return true;
97 case kSRGBA_8888_GrPixelConfig: return true;
98 case kSBGRA_8888_GrPixelConfig: return true;
99 case kRGBA_1010102_GrPixelConfig: return true;
100 case kRGBA_float_GrPixelConfig: return true;
101 case kRG_float_GrPixelConfig: return false;
102 case kAlpha_half_GrPixelConfig: return false;
103 case kRGBA_half_GrPixelConfig: return true;
104 case kRGBA_half_Clamped_GrPixelConfig: return true;
105 case kRGB_ETC1_GrPixelConfig: return false;
106 case kAlpha_8_as_Alpha_GrPixelConfig: return false;
107 case kAlpha_8_as_Red_GrPixelConfig: return false;
108 case kAlpha_half_as_Red_GrPixelConfig: return false;
109 case kGray_8_as_Lum_GrPixelConfig: return false;
110 case kGray_8_as_Red_GrPixelConfig: return false;
Robert Phillipsfe18de52019-06-06 17:21:50 -0400111 // Experimental (for P016 and P010)
112 case kR_16_GrPixelConfig: return false;
113 case kRG_1616_GrPixelConfig: return false;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400114 }
115 SK_ABORT("Invalid GrPixelConfig");
116 return false;
117}
118
119static bool valid_pixel_conversion(GrColorType cpuColorType, GrPixelConfig gpuConfig,
120 bool premulConversion) {
121 // We only allow premul <-> unpremul conversions for some formats
122 if (premulConversion &&
123 (!valid_premul_color_type(cpuColorType) || !valid_premul_config(gpuConfig))) {
124 return false;
125 }
126 return true;
127}
128
129bool GrSurfaceContext::readPixelsImpl(GrContext* direct, int left, int top, int width,
130 int height, GrColorType dstColorType,
131 SkColorSpace* dstColorSpace, void* buffer, size_t rowBytes,
132 uint32_t pixelOpsFlags) {
133 SkASSERT(buffer);
134
135 GrSurfaceProxy* srcProxy = this->asSurfaceProxy();
136
137 // MDB TODO: delay this instantiation until later in the method
138 if (!srcProxy->instantiate(direct->priv().resourceProvider())) {
139 return false;
140 }
141
142 GrSurface* srcSurface = srcProxy->peekSurface();
143
144 if (!GrSurfacePriv::AdjustReadPixelParams(srcSurface->width(), srcSurface->height(),
145 GrColorTypeBytesPerPixel(dstColorType), &left, &top,
146 &width, &height, &buffer, &rowBytes)) {
147 return false;
148 }
149
150 // TODO: Make GrSurfaceContext know its alpha type and pass dst buffer's alpha type.
151 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
152
153 if (!valid_pixel_conversion(dstColorType, srcProxy->config(), unpremul)) {
154 return false;
155 }
156
157 bool needColorConversion =
158 SkColorSpaceXformSteps::Required(this->colorSpaceInfo().colorSpace(), dstColorSpace);
159
160 const GrCaps* caps = direct->priv().caps();
161 // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't
162 // care so much about getImageData performance. However, in order to ensure putImageData/
163 // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary
164 // unpremul step to writeSurfacePixels's premul step (which is determined empirically in
165 // fContext->vaildaPMUPMConversionExists()).
166 bool canvas2DFastPath =
167 unpremul &&
168 !needColorConversion &&
169 (GrColorType::kRGBA_8888 == dstColorType || GrColorType::kBGRA_8888 == dstColorType) &&
170 SkToBool(srcProxy->asTextureProxy()) &&
171 (srcProxy->config() == kRGBA_8888_GrPixelConfig ||
172 srcProxy->config() == kBGRA_8888_GrPixelConfig) &&
173 caps->isConfigRenderable(kRGBA_8888_GrPixelConfig) &&
174 direct->priv().validPMUPMConversionExists();
175
176 if (!caps->surfaceSupportsReadPixels(srcSurface) ||
177 canvas2DFastPath) {
178 GrBackendFormat format;
179 GrPixelConfig config;
180 if (canvas2DFastPath) {
181 config = kRGBA_8888_GrPixelConfig;
182 format = caps->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
183 } else {
184 config = srcProxy->config();
185 format = srcProxy->backendFormat().makeTexture2D();
186 if (!format.isValid()) {
187 return false;
188 }
189 }
190 sk_sp<SkColorSpace> cs = canvas2DFastPath ? nullptr : this->colorSpaceInfo().refColorSpace();
191
192 sk_sp<GrRenderTargetContext> tempCtx = direct->priv().makeDeferredRenderTargetContext(
193 format, SkBackingFit::kApprox, width, height, config, std::move(cs), 1,
194 GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin, nullptr, SkBudgeted::kYes);
195 if (!tempCtx) {
196 return false;
197 }
198
199 std::unique_ptr<GrFragmentProcessor> fp;
200 if (canvas2DFastPath) {
201 fp = direct->priv().createPMToUPMEffect(
202 GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()),
203 SkMatrix::I()));
204 if (dstColorType == GrColorType::kBGRA_8888) {
205 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
206 dstColorType = GrColorType::kRGBA_8888;
207 }
208 } else {
209 fp = GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()), SkMatrix::I());
210 }
211 if (!fp) {
212 return false;
213 }
214 GrPaint paint;
215 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
216 paint.addColorFragmentProcessor(std::move(fp));
217
218 tempCtx->asRenderTargetContext()->fillRectToRect(
219 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
220 SkRect::MakeWH(width, height), SkRect::MakeXYWH(left, top, width, height));
221
222 uint32_t flags = canvas2DFastPath ? 0 : pixelOpsFlags;
223 return tempCtx->readPixelsImpl(direct, 0, 0, width, height, dstColorType, dstColorSpace,
224 buffer, rowBytes, flags);
225 }
226
227 bool convert = unpremul || needColorConversion;
228
229 bool flip = srcProxy->origin() == kBottomLeft_GrSurfaceOrigin;
230 if (flip) {
231 top = srcSurface->height() - top - height;
232 }
233
234 GrColorType allowedColorType = caps->supportedReadPixelsColorType(srcProxy->config(),
235 dstColorType);
236 convert = convert || (dstColorType != allowedColorType);
237
238 SkAutoPixmapStorage tempPixmap;
239 SkPixmap finalPixmap;
240 if (convert) {
241 SkColorType srcSkColorType = GrColorTypeToSkColorType(allowedColorType);
242 SkColorType dstSkColorType = GrColorTypeToSkColorType(dstColorType);
243 bool srcAlwaysOpaque = SkColorTypeIsAlwaysOpaque(srcSkColorType);
244 bool dstAlwaysOpaque = SkColorTypeIsAlwaysOpaque(dstSkColorType);
245 if (kUnknown_SkColorType == srcSkColorType || kUnknown_SkColorType == dstSkColorType) {
246 return false;
247 }
248 auto tempAT = srcAlwaysOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
249 auto tempII = SkImageInfo::Make(width, height, srcSkColorType, tempAT,
250 this->colorSpaceInfo().refColorSpace());
251 SkASSERT(!unpremul || !dstAlwaysOpaque);
252 auto finalAT = (srcAlwaysOpaque || dstAlwaysOpaque)
253 ? kOpaque_SkAlphaType
254 : unpremul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType;
255 auto finalII =
256 SkImageInfo::Make(width, height, dstSkColorType, finalAT, sk_ref_sp(dstColorSpace));
257 if (!SkImageInfoValidConversion(finalII, tempII)) {
258 return false;
259 }
260 if (!tempPixmap.tryAlloc(tempII)) {
261 return false;
262 }
263 finalPixmap.reset(finalII, buffer, rowBytes);
264 buffer = tempPixmap.writable_addr();
265 rowBytes = tempPixmap.rowBytes();
266 // Chrome msan bots require this.
267 sk_bzero(buffer, tempPixmap.computeByteSize());
268 }
269
270 direct->priv().flushSurface(srcProxy);
271
272 if (!direct->priv().getGpu()->readPixels(srcSurface, left, top, width, height, allowedColorType,
273 buffer, rowBytes)) {
274 return false;
275 }
276
277 if (flip) {
278 size_t trimRowBytes = GrColorTypeBytesPerPixel(allowedColorType) * width;
279 std::unique_ptr<char[]> row(new char[trimRowBytes]);
280 char* upper = reinterpret_cast<char*>(buffer);
281 char* lower = reinterpret_cast<char*>(buffer) + (height - 1) * rowBytes;
282 for (int y = 0; y < height / 2; ++y, upper += rowBytes, lower -= rowBytes) {
283 memcpy(row.get(), upper, trimRowBytes);
284 memcpy(upper, lower, trimRowBytes);
285 memcpy(lower, row.get(), trimRowBytes);
286 }
287 }
288 if (convert) {
289 if (!tempPixmap.readPixels(finalPixmap)) {
290 return false;
291 }
292 }
293 return true;
294}
Robert Phillips0d075de2019-03-04 11:08:13 -0500295
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400296bool GrSurfaceContext::readPixels(const SkImageInfo& dstInfo, void* dstBuffer,
297 size_t dstRowBytes, int x, int y, uint32_t flags) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400298 ASSERT_SINGLE_OWNER
299 RETURN_FALSE_IF_ABANDONED
300 SkDEBUGCODE(this->validate();)
Robert Phillips0d075de2019-03-04 11:08:13 -0500301 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::readPixels");
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400302
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400303 // TODO: this seems to duplicate code in SkImage_Gpu::onReadPixels
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400304 if (kUnpremul_SkAlphaType == dstInfo.alphaType() &&
305 !GrPixelConfigIsOpaque(this->asSurfaceProxy()->config())) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400306 flags |= kUnpremul_PixelOpsFlag;
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400307 }
Brian Salomonc320b152018-02-20 14:05:36 -0500308 auto colorType = SkColorTypeToGrColorType(dstInfo.colorType());
309 if (GrColorType::kUnknown == colorType) {
310 return false;
311 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500312
313 auto direct = fContext->priv().asDirectContext();
314 if (!direct) {
315 return false;
316 }
317
Greg Daniel6eb8c242019-06-05 10:22:24 -0400318 return this->readPixelsImpl(direct, x, y, dstInfo.width(), dstInfo.height(), colorType,
319 dstInfo.colorSpace(), dstBuffer, dstRowBytes, flags);
320}
321
322bool GrSurfaceContext::writePixelsImpl(GrContext* direct, int left, int top, int width, int height,
323 GrColorType srcColorType, SkColorSpace* srcColorSpace,
324 const void* srcBuffer, size_t srcRowBytes,
325 uint32_t pixelOpsFlags) {
326 if (GrColorType::kUnknown == srcColorType) {
327 return false;
328 }
329
330 GrSurfaceProxy* dstProxy = this->asSurfaceProxy();
331 if (!dstProxy->instantiate(direct->priv().resourceProvider())) {
332 return false;
333 }
334
335 GrSurface* dstSurface = dstProxy->peekSurface();
336
337 if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(),
338 GrColorTypeBytesPerPixel(srcColorType), &left, &top,
339 &width, &height, &srcBuffer, &srcRowBytes)) {
340 return false;
341 }
342
343 // TODO: Make GrSurfaceContext know its alpha type and pass src buffer's alpha type.
344 bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
345
346 bool needColorConversion =
347 SkColorSpaceXformSteps::Required(srcColorSpace, this->colorSpaceInfo().colorSpace());
348
349 const GrCaps* caps = direct->priv().caps();
350 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
351 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
352 bool canvas2DFastPath =
353 !caps->avoidWritePixelsFastPath() &&
354 premul &&
355 !needColorConversion &&
356 (srcColorType == GrColorType::kRGBA_8888 || srcColorType == GrColorType::kBGRA_8888) &&
357 SkToBool(this->asRenderTargetContext()) &&
358 (dstProxy->config() == kRGBA_8888_GrPixelConfig ||
359 dstProxy->config() == kBGRA_8888_GrPixelConfig) &&
360 direct->priv().caps()->isConfigTexturable(kRGBA_8888_GrPixelConfig) &&
361 direct->priv().validPMUPMConversionExists();
362
363 if (!caps->surfaceSupportsWritePixels(dstSurface) || canvas2DFastPath) {
364 GrSurfaceDesc desc;
365 desc.fWidth = width;
366 desc.fHeight = height;
367 desc.fSampleCnt = 1;
368
369 GrBackendFormat format;
370 if (canvas2DFastPath) {
371 desc.fConfig = kRGBA_8888_GrPixelConfig;
372 format = caps->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
373 } else {
374 desc.fConfig = dstProxy->config();
375 format = dstProxy->backendFormat().makeTexture2D();
376 if (!format.isValid()) {
377 return false;
378 }
379 }
380
381 auto tempProxy = direct->priv().proxyProvider()->createProxy(
Greg Daniel46cfbc62019-06-07 11:43:30 -0400382 format, desc, dstProxy->origin(), SkBackingFit::kApprox, SkBudgeted::kYes);
Greg Daniel6eb8c242019-06-05 10:22:24 -0400383 if (!tempProxy) {
384 return false;
385 }
386 auto tempCtx = direct->priv().drawingManager()->makeTextureContext(
387 tempProxy, this->colorSpaceInfo().refColorSpace());
388 if (!tempCtx) {
389 return false;
390 }
391 uint32_t flags = canvas2DFastPath ? 0 : pixelOpsFlags;
392
393 // In the fast path we always write the srcData to the temp context as though it were RGBA.
394 // When the data is really BGRA the write will cause the R and B channels to be swapped in
395 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
396 // dst.
397 auto tmpColorType = canvas2DFastPath ? GrColorType::kRGBA_8888 : srcColorType;
398 if (!tempCtx->writePixelsImpl(direct, 0, 0, width, height, tmpColorType, srcColorSpace,
399 srcBuffer, srcRowBytes, flags)) {
400 return false;
401 }
402
403 if (this->asRenderTargetContext()) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400404 std::unique_ptr<GrFragmentProcessor> fp;
Greg Daniel6eb8c242019-06-05 10:22:24 -0400405 if (canvas2DFastPath) {
406 fp = direct->priv().createUPMToPMEffect(
407 GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I()));
408 if (srcColorType == GrColorType::kBGRA_8888) {
409 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
410 }
411 } else {
412 fp = GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I());
413 }
414 if (!fp) {
415 return false;
416 }
417 GrPaint paint;
418 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
419 paint.addColorFragmentProcessor(std::move(fp));
420 this->asRenderTargetContext()->fillRectToRect(
421 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
422 SkRect::MakeXYWH(left, top, width, height), SkRect::MakeWH(width, height));
423 } else {
424 SkIRect srcRect = SkIRect::MakeWH(width, height);
425 SkIPoint dstPoint = SkIPoint::Make(left, top);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400426 if (!this->copy(tempProxy.get(), srcRect, dstPoint)) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400427 return false;
428 }
Greg Daniel6eb8c242019-06-05 10:22:24 -0400429 }
430 return true;
431 }
432
433 bool convert = premul || needColorConversion;
434
435 if (!valid_pixel_conversion(srcColorType, dstProxy->config(), premul)) {
436 return false;
437 }
438
439 GrColorType allowedColorType = caps->supportedWritePixelsColorType(dstProxy->config(),
440 srcColorType);
441 convert = convert || (srcColorType != allowedColorType);
442
443 std::unique_ptr<char[]> tempBuffer;
444 if (convert) {
445 auto srcSkColorType = GrColorTypeToSkColorType(srcColorType);
446 auto dstSkColorType = GrColorTypeToSkColorType(allowedColorType);
447 if (kUnknown_SkColorType == srcSkColorType || kUnknown_SkColorType == dstSkColorType) {
448 return false;
449 }
450 auto srcAlphaType = SkColorTypeIsAlwaysOpaque(srcSkColorType)
451 ? kOpaque_SkAlphaType
452 : (premul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType);
453 SkPixmap src(SkImageInfo::Make(width, height, srcSkColorType, srcAlphaType,
454 sk_ref_sp(srcColorSpace)),
455 srcBuffer, srcRowBytes);
456 auto tempSrcII = SkImageInfo::Make(width, height, dstSkColorType, kPremul_SkAlphaType,
457 this->colorSpaceInfo().refColorSpace());
458 auto size = tempSrcII.computeMinByteSize();
459 if (!size) {
460 return false;
461 }
462 tempBuffer.reset(new char[size]);
463 SkPixmap tempSrc(tempSrcII, tempBuffer.get(), tempSrcII.minRowBytes());
464 if (!src.readPixels(tempSrc)) {
465 return false;
466 }
467 srcColorType = allowedColorType;
468 srcBuffer = tempSrc.addr();
469 srcRowBytes = tempSrc.rowBytes();
470 if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
471 std::unique_ptr<char[]> row(new char[srcRowBytes]);
472 for (int y = 0; y < height / 2; ++y) {
473 memcpy(row.get(), tempSrc.addr(0, y), srcRowBytes);
474 memcpy(tempSrc.writable_addr(0, y), tempSrc.addr(0, height - 1 - y), srcRowBytes);
475 memcpy(tempSrc.writable_addr(0, height - 1 - y), row.get(), srcRowBytes);
476 }
477 top = dstSurface->height() - top - height;
478 }
479 } else if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
480 size_t trimRowBytes = GrColorTypeBytesPerPixel(srcColorType) * width;
481 tempBuffer.reset(new char[trimRowBytes * height]);
482 char* dst = reinterpret_cast<char*>(tempBuffer.get()) + trimRowBytes * (height - 1);
483 const char* src = reinterpret_cast<const char*>(srcBuffer);
484 for (int i = 0; i < height; ++i, src += srcRowBytes, dst -= trimRowBytes) {
485 memcpy(dst, src, trimRowBytes);
486 }
487 srcBuffer = tempBuffer.get();
488 srcRowBytes = trimRowBytes;
489 top = dstSurface->height() - top - height;
490 }
491
492 // On platforms that prefer flushes over VRAM use (i.e., ANGLE) we're better off forcing a
493 // complete flush here. On platforms that prefer VRAM use over flushes we're better off
494 // giving the drawing manager the chance of skipping the flush (i.e., by passing in the
495 // destination proxy)
496 // TODO: should this policy decision just be moved into the drawing manager?
497 direct->priv().flushSurface(caps->preferVRAMUseOverFlushes() ? dstProxy : nullptr);
498
499 return direct->priv().getGpu()->writePixels(dstSurface, left, top, width, height, srcColorType,
500 srcBuffer, srcRowBytes);
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400501}
502
503bool GrSurfaceContext::writePixels(const SkImageInfo& srcInfo, const void* srcBuffer,
504 size_t srcRowBytes, int x, int y, uint32_t flags) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400505 ASSERT_SINGLE_OWNER
506 RETURN_FALSE_IF_ABANDONED
507 SkDEBUGCODE(this->validate();)
Robert Phillips0d075de2019-03-04 11:08:13 -0500508 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::writePixels");
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400509
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400510 if (kUnpremul_SkAlphaType == srcInfo.alphaType()) {
Greg Daniel6eb8c242019-06-05 10:22:24 -0400511 flags |= kUnpremul_PixelOpsFlag;
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400512 }
Brian Salomonc320b152018-02-20 14:05:36 -0500513 auto colorType = SkColorTypeToGrColorType(srcInfo.colorType());
514 if (GrColorType::kUnknown == colorType) {
515 return false;
516 }
Robert Phillips6a6de562019-02-15 15:19:15 -0500517
518 auto direct = fContext->priv().asDirectContext();
519 if (!direct) {
520 return false;
521 }
522
Greg Danielb3f82dd2019-05-29 14:24:32 -0400523 if (this->asSurfaceProxy()->readOnly()) {
524 return false;
525 }
526
Greg Daniel6eb8c242019-06-05 10:22:24 -0400527 return this->writePixelsImpl(direct, x, y, srcInfo.width(), srcInfo.height(), colorType,
528 srcInfo.colorSpace(), srcBuffer, srcRowBytes, flags);
Brian Osman45580d32016-11-23 09:37:01 -0500529}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400530
531bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
532 ASSERT_SINGLE_OWNER
533 RETURN_FALSE_IF_ABANDONED
534 SkDEBUGCODE(this->validate();)
Greg Daniel46cfbc62019-06-07 11:43:30 -0400535 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContextPriv::copy");
Greg Daniel25af6712018-04-25 10:44:38 -0400536
Greg Daniel46cfbc62019-06-07 11:43:30 -0400537 SkASSERT(src->backendFormat().textureType() != GrTextureType::kExternal);
538 SkASSERT(src->origin() == this->asSurfaceProxy()->origin());
539
540 GrSurfaceProxy* dst = this->asSurfaceProxy();
541
542 if (!fContext->priv().caps()->canCopySurface(dst, src, srcRect, dstPoint)) {
Greg Daniel25af6712018-04-25 10:44:38 -0400543 return false;
544 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400545
Greg Daniel46cfbc62019-06-07 11:43:30 -0400546 return this->getOpList()->copySurface(fContext, dst, src, srcRect, dstPoint);
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400547}
Greg Daniel46cfbc62019-06-07 11:43:30 -0400548