1
2 /*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10 #include "GrRenderTarget.h"
11
12 #include "GrContext.h"
13 #include "GrGpu.h"
14 #include "GrStencilBuffer.h"
15
SK_DEFINE_INST_COUNT(GrRenderTarget)16 SK_DEFINE_INST_COUNT(GrRenderTarget)
17
18 bool GrRenderTarget::readPixels(int left, int top, int width, int height,
19 GrPixelConfig config,
20 void* buffer,
21 size_t rowBytes,
22 uint32_t pixelOpsFlags) {
23 // go through context so that all necessary flushing occurs
24 GrContext* context = this->getContext();
25 if (NULL == context) {
26 return false;
27 }
28 return context->readRenderTargetPixels(this,
29 left, top, width, height,
30 config, buffer, rowBytes,
31 pixelOpsFlags);
32 }
33
writePixels(int left,int top,int width,int height,GrPixelConfig config,const void * buffer,size_t rowBytes,uint32_t pixelOpsFlags)34 void GrRenderTarget::writePixels(int left, int top, int width, int height,
35 GrPixelConfig config,
36 const void* buffer,
37 size_t rowBytes,
38 uint32_t pixelOpsFlags) {
39 // go through context so that all necessary flushing occurs
40 GrContext* context = this->getContext();
41 if (NULL == context) {
42 return;
43 }
44 context->writeRenderTargetPixels(this,
45 left, top, width, height,
46 config, buffer, rowBytes,
47 pixelOpsFlags);
48 }
49
resolve()50 void GrRenderTarget::resolve() {
51 // go through context so that all necessary flushing occurs
52 GrContext* context = this->getContext();
53 if (NULL == context) {
54 return;
55 }
56 context->resolveRenderTarget(this);
57 }
58
sizeInBytes() const59 size_t GrRenderTarget::sizeInBytes() const {
60 int colorBits;
61 if (kUnknown_GrPixelConfig == fDesc.fConfig) {
62 colorBits = 32; // don't know, make a guess
63 } else {
64 colorBits = GrBytesPerPixel(fDesc.fConfig);
65 }
66 uint64_t size = fDesc.fWidth;
67 size *= fDesc.fHeight;
68 size *= colorBits;
69 size *= GrMax(1, fDesc.fSampleCnt);
70 return (size_t)(size / 8);
71 }
72
flagAsNeedingResolve(const GrIRect * rect)73 void GrRenderTarget::flagAsNeedingResolve(const GrIRect* rect) {
74 if (kCanResolve_ResolveType == getResolveType()) {
75 if (NULL != rect) {
76 fResolveRect.join(*rect);
77 if (!fResolveRect.intersect(0, 0, this->width(), this->height())) {
78 fResolveRect.setEmpty();
79 }
80 } else {
81 fResolveRect.setLTRB(0, 0, this->width(), this->height());
82 }
83 }
84 }
85
overrideResolveRect(const GrIRect rect)86 void GrRenderTarget::overrideResolveRect(const GrIRect rect) {
87 fResolveRect = rect;
88 if (fResolveRect.isEmpty()) {
89 fResolveRect.setLargestInverted();
90 } else {
91 if (!fResolveRect.intersect(0, 0, this->width(), this->height())) {
92 fResolveRect.setLargestInverted();
93 }
94 }
95 }
96
setStencilBuffer(GrStencilBuffer * stencilBuffer)97 void GrRenderTarget::setStencilBuffer(GrStencilBuffer* stencilBuffer) {
98 if (stencilBuffer == fStencilBuffer) {
99 return;
100 }
101
102 if (NULL != fStencilBuffer) {
103 fStencilBuffer->unref();
104
105 GrContext* context = this->getContext();
106 if (NULL != context) {
107 context->purgeCache();
108 }
109
110 if (NULL != context) {
111 context->purgeCache();
112 }
113 }
114
115 fStencilBuffer = stencilBuffer;
116
117 if (NULL != fStencilBuffer) {
118 fStencilBuffer->ref();
119 }
120 }
121
onRelease()122 void GrRenderTarget::onRelease() {
123 this->setStencilBuffer(NULL);
124
125 INHERITED::onRelease();
126 }
127
onAbandon()128 void GrRenderTarget::onAbandon() {
129 this->setStencilBuffer(NULL);
130
131 INHERITED::onAbandon();
132 }
133