Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / src / gpu / GrRenderTarget.cpp
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
16 bool GrRenderTarget::readPixels(int left, int top, int width, int height,
17                                 GrPixelConfig config,
18                                 void* buffer,
19                                 size_t rowBytes,
20                                 uint32_t pixelOpsFlags) {
21     // go through context so that all necessary flushing occurs
22     GrContext* context = this->getContext();
23     if (NULL == context) {
24         return false;
25     }
26     return context->readRenderTargetPixels(this,
27                                            left, top, width, height,
28                                            config, buffer, rowBytes,
29                                            pixelOpsFlags);
30 }
31
32 void GrRenderTarget::writePixels(int left, int top, int width, int height,
33                                  GrPixelConfig config,
34                                  const void* buffer,
35                                  size_t rowBytes,
36                                  uint32_t pixelOpsFlags) {
37     // go through context so that all necessary flushing occurs
38     GrContext* context = this->getContext();
39     if (NULL == context) {
40         return;
41     }
42     context->writeRenderTargetPixels(this,
43                                      left, top, width, height,
44                                      config, buffer, rowBytes,
45                                      pixelOpsFlags);
46 }
47
48 void GrRenderTarget::resolve() {
49     // go through context so that all necessary flushing occurs
50     GrContext* context = this->getContext();
51     if (NULL == context) {
52         return;
53     }
54     context->resolveRenderTarget(this);
55 }
56
57 void GrRenderTarget::discard() {
58     // go through context so that all necessary flushing occurs
59     GrContext* context = this->getContext();
60     if (NULL == context) {
61         return;
62     }
63     context->discardRenderTarget(this);
64 }
65
66 size_t GrRenderTarget::gpuMemorySize() const {
67     size_t colorBits;
68     if (kUnknown_GrPixelConfig == fDesc.fConfig) {
69         colorBits = 32; // don't know, make a guess
70     } else {
71         colorBits = GrBytesPerPixel(fDesc.fConfig) * 8;
72     }
73     uint64_t size = fDesc.fWidth;
74     size *= fDesc.fHeight;
75     size *= colorBits;
76     size *= SkTMax(1, fDesc.fSampleCnt);
77     return (size_t)(size / 8);
78 }
79
80 void GrRenderTarget::flagAsNeedingResolve(const SkIRect* rect) {
81     if (kCanResolve_ResolveType == getResolveType()) {
82         if (NULL != rect) {
83             fResolveRect.join(*rect);
84             if (!fResolveRect.intersect(0, 0, this->width(), this->height())) {
85                 fResolveRect.setEmpty();
86             }
87         } else {
88             fResolveRect.setLTRB(0, 0, this->width(), this->height());
89         }
90     }
91 }
92
93 void GrRenderTarget::overrideResolveRect(const SkIRect rect) {
94     fResolveRect = rect;
95     if (fResolveRect.isEmpty()) {
96         fResolveRect.setLargestInverted();
97     } else {
98         if (!fResolveRect.intersect(0, 0, this->width(), this->height())) {
99             fResolveRect.setLargestInverted();
100         }
101     }
102 }
103
104 void GrRenderTarget::setStencilBuffer(GrStencilBuffer* stencilBuffer) {
105     SkRefCnt_SafeAssign(fStencilBuffer, stencilBuffer);
106 }
107
108 void GrRenderTarget::onRelease() {
109     this->setStencilBuffer(NULL);
110
111     INHERITED::onRelease();
112 }
113
114 void GrRenderTarget::onAbandon() {
115     this->setStencilBuffer(NULL);
116
117     INHERITED::onAbandon();
118 }