blob: ae44eb9fafae8bbc5609972c3f9d3709425b9fab [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 Phillips69893702019-02-22 11:16:30 -050012#include "GrRecordingContext.h"
13#include "GrRecordingContextPriv.h"
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040014#include "SkGr.h"
Brian Osman45580d32016-11-23 09:37:01 -050015#include "../private/GrAuditTrail.h"
16
Robert Phillips2de8cfa2017-06-28 10:33:41 -040017#define ASSERT_SINGLE_OWNER \
18 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
Robert Phillips69893702019-02-22 11:16:30 -050019#define RETURN_FALSE_IF_ABANDONED if (this->fContext->priv().abandoned()) { return false; }
Brian Osman45580d32016-11-23 09:37:01 -050020
21// In MDB mode the reffing of the 'getLastOpList' call's result allows in-progress
22// GrOpLists to be picked up and added to by renderTargetContexts lower in the call
23// stack. When this occurs with a closed GrOpList, a new one will be allocated
24// when the renderTargetContext attempts to use it (via getOpList).
Robert Phillips69893702019-02-22 11:16:30 -050025GrSurfaceContext::GrSurfaceContext(GrRecordingContext* context,
Brian Salomonf3569f02017-10-24 12:52:33 -040026 GrPixelConfig config,
Robert Phillips0d075de2019-03-04 11:08:13 -050027 sk_sp<SkColorSpace> colorSpace)
Brian Salomonf3569f02017-10-24 12:52:33 -040028 : fContext(context)
Robert Phillips0d075de2019-03-04 11:08:13 -050029 , fColorSpaceInfo(std::move(colorSpace), config) {
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040030}
31
Robert Phillips0d075de2019-03-04 11:08:13 -050032GrAuditTrail* GrSurfaceContext::auditTrail() {
33 return fContext->priv().auditTrail();
34}
35
36GrDrawingManager* GrSurfaceContext::drawingManager() {
37 return fContext->priv().drawingManager();
38}
39
40const GrDrawingManager* GrSurfaceContext::drawingManager() const {
41 return fContext->priv().drawingManager();
42}
43
44#ifdef SK_DEBUG
45GrSingleOwner* GrSurfaceContext::singleOwner() {
46 return fContext->priv().singleOwner();
47}
48#endif
49
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040050bool GrSurfaceContext::readPixels(const SkImageInfo& dstInfo, void* dstBuffer,
51 size_t dstRowBytes, int x, int y, uint32_t flags) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -040052 ASSERT_SINGLE_OWNER
53 RETURN_FALSE_IF_ABANDONED
54 SkDEBUGCODE(this->validate();)
Robert Phillips0d075de2019-03-04 11:08:13 -050055 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::readPixels");
Robert Phillips2de8cfa2017-06-28 10:33:41 -040056
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040057 // TODO: this seems to duplicate code in SkImage_Gpu::onReadPixels
Brian Salomon5fba7ad2018-03-22 10:01:16 -040058 if (kUnpremul_SkAlphaType == dstInfo.alphaType() &&
59 !GrPixelConfigIsOpaque(this->asSurfaceProxy()->config())) {
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040060 flags |= GrContextPriv::kUnpremul_PixelOpsFlag;
61 }
Brian Salomonc320b152018-02-20 14:05:36 -050062 auto colorType = SkColorTypeToGrColorType(dstInfo.colorType());
63 if (GrColorType::kUnknown == colorType) {
64 return false;
65 }
Robert Phillips6a6de562019-02-15 15:19:15 -050066
67 auto direct = fContext->priv().asDirectContext();
68 if (!direct) {
69 return false;
70 }
71
72 return direct->priv().readSurfacePixels(this, x, y, dstInfo.width(), dstInfo.height(),
73 colorType, dstInfo.colorSpace(), dstBuffer,
74 dstRowBytes, flags);
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040075}
76
77bool GrSurfaceContext::writePixels(const SkImageInfo& srcInfo, const void* srcBuffer,
78 size_t srcRowBytes, int x, int y, uint32_t flags) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -040079 ASSERT_SINGLE_OWNER
80 RETURN_FALSE_IF_ABANDONED
81 SkDEBUGCODE(this->validate();)
Robert Phillips0d075de2019-03-04 11:08:13 -050082 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::writePixels");
Robert Phillips2de8cfa2017-06-28 10:33:41 -040083
Robert Phillipsa90aa2b2017-04-10 08:19:26 -040084 if (kUnpremul_SkAlphaType == srcInfo.alphaType()) {
85 flags |= GrContextPriv::kUnpremul_PixelOpsFlag;
86 }
Brian Salomonc320b152018-02-20 14:05:36 -050087 auto colorType = SkColorTypeToGrColorType(srcInfo.colorType());
88 if (GrColorType::kUnknown == colorType) {
89 return false;
90 }
Robert Phillips6a6de562019-02-15 15:19:15 -050091
92 auto direct = fContext->priv().asDirectContext();
93 if (!direct) {
94 return false;
95 }
96
97 return direct->priv().writeSurfacePixels(this, x, y, srcInfo.width(), srcInfo.height(),
98 colorType, srcInfo.colorSpace(), srcBuffer,
99 srcRowBytes, flags);
Brian Osman45580d32016-11-23 09:37:01 -0500100}
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400101
102bool GrSurfaceContext::copy(GrSurfaceProxy* src, const SkIRect& srcRect, const SkIPoint& dstPoint) {
103 ASSERT_SINGLE_OWNER
104 RETURN_FALSE_IF_ABANDONED
105 SkDEBUGCODE(this->validate();)
Robert Phillips0d075de2019-03-04 11:08:13 -0500106 GR_AUDIT_TRAIL_AUTO_FRAME(this->auditTrail(), "GrSurfaceContext::copy");
Greg Daniel25af6712018-04-25 10:44:38 -0400107
Robert Phillips9da87e02019-02-04 13:26:26 -0500108 if (!fContext->priv().caps()->canCopySurface(this->asSurfaceProxy(), src, srcRect,
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400109 dstPoint)) {
Greg Daniel25af6712018-04-25 10:44:38 -0400110 return false;
111 }
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400112
Robert Phillips7c525e62018-06-12 10:11:12 -0400113 return this->getOpList()->copySurface(fContext, this->asSurfaceProxy(),
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400114 src, srcRect, dstPoint);
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400115}