blob: a77adddac03e0cddf95ce95718602d5c8c6e9bf4 [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
8#include "GrSurfaceContext.h"
Robert Phillipsa90aa2b2017-04-10 08:19:26 -04009#include "GrContextPriv.h"
Robert Phillips2de8cfa2017-06-28 10:33:41 -040010#include "GrDrawingManager.h"
11#include "GrOpList.h"
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040012#include "SkGr.h"
Brian Osman45580d32016-11-23 09:37:01 -050013#include "../private/GrAuditTrail.h"
14
Robert Phillips2de8cfa2017-06-28 10:33:41 -040015#define ASSERT_SINGLE_OWNER \
16 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
17#define RETURN_FALSE_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return false; }
Brian Osman45580d32016-11-23 09:37:01 -050018
19// In MDB mode the reffing of the 'getLastOpList' call's result allows in-progress
20// GrOpLists to be picked up and added to by renderTargetContexts lower in the call
21// stack. When this occurs with a closed GrOpList, a new one will be allocated
22// when the renderTargetContext attempts to use it (via getOpList).
23GrSurfaceContext::GrSurfaceContext(GrContext* context,
Robert Phillips72152832017-01-25 17:31:35 -050024 GrDrawingManager* drawingMgr,
Brian Salomonf3569f02017-10-24 12:52:33 -040025 GrPixelConfig config,
Robert Phillips2c862492017-01-18 10:08:39 -050026 sk_sp<SkColorSpace> colorSpace,
Brian Osman45580d32016-11-23 09:37:01 -050027 GrAuditTrail* auditTrail,
28 GrSingleOwner* singleOwner)
Brian Salomonf3569f02017-10-24 12:52:33 -040029 : fContext(context)
30 , fAuditTrail(auditTrail)
31 , fColorSpaceInfo(std::move(colorSpace), config)
32 , fDrawingManager(drawingMgr)
Brian Osman45580d32016-11-23 09:37:01 -050033#ifdef SK_DEBUG
Brian Salomonf3569f02017-10-24 12:52:33 -040034 , fSingleOwner(singleOwner)
Brian Osman45580d32016-11-23 09:37:01 -050035#endif
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040036{
37}
38
39bool GrSurfaceContext::readPixels(const SkImageInfo& dstInfo, void* dstBuffer,
40 size_t dstRowBytes, int x, int y, uint32_t flags) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -040041 ASSERT_SINGLE_OWNER
42 RETURN_FALSE_IF_ABANDONED
43 SkDEBUGCODE(this->validate();)
44 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrSurfaceContext::readPixels");
45
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040046 // TODO: this seems to duplicate code in SkImage_Gpu::onReadPixels
Brian Salomon5fba7ad2018-03-22 10:01:16 -040047 if (kUnpremul_SkAlphaType == dstInfo.alphaType() &&
48 !GrPixelConfigIsOpaque(this->asSurfaceProxy()->config())) {
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040049 flags |= GrContextPriv::kUnpremul_PixelOpsFlag;
50 }
Brian Salomonc320b152018-02-20 14:05:36 -050051 auto colorType = SkColorTypeToGrColorType(dstInfo.colorType());
52 if (GrColorType::kUnknown == colorType) {
53 return false;
54 }
55 return fContext->contextPriv().readSurfacePixels(this, x, y, dstInfo.width(), dstInfo.height(),
56 colorType, dstInfo.colorSpace(), dstBuffer,
57 dstRowBytes, flags);
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040058}
59
60bool GrSurfaceContext::writePixels(const SkImageInfo& srcInfo, const void* srcBuffer,
61 size_t srcRowBytes, int x, int y, uint32_t flags) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -040062 ASSERT_SINGLE_OWNER
63 RETURN_FALSE_IF_ABANDONED
64 SkDEBUGCODE(this->validate();)
65 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrSurfaceContext::writePixels");
66
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040067 if (kUnpremul_SkAlphaType == srcInfo.alphaType()) {
68 flags |= GrContextPriv::kUnpremul_PixelOpsFlag;
69 }
Brian Salomonc320b152018-02-20 14:05:36 -050070 auto colorType = SkColorTypeToGrColorType(srcInfo.colorType());
71 if (GrColorType::kUnknown == colorType) {
72 return false;
73 }
74 return fContext->contextPriv().writeSurfacePixels(this, x, y, srcInfo.width(), srcInfo.height(),
75 colorType, srcInfo.colorSpace(), srcBuffer,
76 srcRowBytes, flags);
Brian Osman45580d32016-11-23 09:37:01 -050077}
Robert Phillips2de8cfa2017-06-28 10:33:41 -040078
79bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
80 ASSERT_SINGLE_OWNER
81 RETURN_FALSE_IF_ABANDONED
82 SkDEBUGCODE(this->validate();)
Greg Daniel25af6712018-04-25 10:44:38 -040083 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrSurfaceContext::copy");
84
Brian Salomonc7fe0f72018-05-11 10:14:21 -040085 if (!fContext->contextPriv().caps()->canCopySurface(this->asSurfaceProxy(), src, srcRect,
86 dstPoint)) {
Greg Daniel25af6712018-04-25 10:44:38 -040087 return false;
88 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -040089
Robert Phillips7c525e62018-06-12 10:11:12 -040090 return this->getOpList()->copySurface(fContext, this->asSurfaceProxy(),
Brian Salomonc7fe0f72018-05-11 10:14:21 -040091 src, srcRect, dstPoint);
Robert Phillips2de8cfa2017-06-28 10:33:41 -040092}