blob: 7901648d3a8f1ead0fb9fd50860aee184890ebd7 [file] [log] [blame]
bsalomon@google.comaa5b6732011-07-29 15:13:20 +00001
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"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000014#include "GrStencilBuffer.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000015
16bool GrRenderTarget::readPixels(int left, int top, int width, int height,
bsalomon@google.com6f379512011-11-16 20:36:03 +000017 GrPixelConfig config, void* buffer,
18 size_t rowBytes) {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000019 // go through context so that all necessary flushing occurs
bsalomon@google.com6f379512011-11-16 20:36:03 +000020 GrContext* context = this->getContext();
21 if (NULL == context) {
22 return false;
23 }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000024 return context->readRenderTargetPixels(this,
bsalomon@google.com6f379512011-11-16 20:36:03 +000025 left, top,
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000026 width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +000027 config, buffer, rowBytes);
28}
29
30void GrRenderTarget::writePixels(int left, int top, int width, int height,
31 GrPixelConfig config, const void* buffer,
32 size_t rowBytes) {
33 // go through context so that all necessary flushing occurs
34 GrContext* context = this->getContext();
35 if (NULL == context) {
36 return;
37 }
38 context->writeRenderTargetPixels(this,
39 left, top,
40 width, height,
41 config, buffer, rowBytes);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000042}
43
44size_t GrRenderTarget::sizeInBytes() const {
45 int colorBits;
46 if (kUnknown_GrPixelConfig == fConfig) {
47 colorBits = 32; // don't know, make a guess
48 } else {
49 colorBits = GrBytesPerPixel(fConfig);
50 }
bsalomon@google.com99621082011-11-15 16:47:16 +000051 uint64_t size = fWidth;
52 size *= fHeight;
bsalomon@google.comf6ff4952011-08-09 13:32:14 +000053 size *= colorBits;
54 size *= GrMax(1,fSampleCnt);
55 return (size_t)(size / 8);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000056}
57
58void GrRenderTarget::flagAsNeedingResolve(const GrIRect* rect) {
59 if (kCanResolve_ResolveType == getResolveType()) {
60 if (NULL != rect) {
61 fResolveRect.join(*rect);
62 if (!fResolveRect.intersect(0, 0, this->width(), this->height())) {
63 fResolveRect.setEmpty();
64 }
65 } else {
66 fResolveRect.setLTRB(0, 0, this->width(), this->height());
67 }
68 }
69}
70
71void GrRenderTarget::overrideResolveRect(const GrIRect rect) {
72 fResolveRect = rect;
73 if (fResolveRect.isEmpty()) {
74 fResolveRect.setLargestInverted();
75 } else {
76 if (!fResolveRect.intersect(0, 0, this->width(), this->height())) {
77 fResolveRect.setLargestInverted();
78 }
79 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000080}
81
82void GrRenderTarget::setStencilBuffer(GrStencilBuffer* stencilBuffer) {
bsalomon@google.com558a75b2011-08-08 17:01:14 +000083 if (NULL != fStencilBuffer) {
84 fStencilBuffer->wasDetachedFromRenderTarget(this);
85 fStencilBuffer->unref();
86 }
87 fStencilBuffer = stencilBuffer;
88 if (NULL != fStencilBuffer) {
89 fStencilBuffer->wasAttachedToRenderTarget(this);
90 fStencilBuffer->ref();
91 }
bsalomon@google.comf6ff4952011-08-09 13:32:14 +000092}