1aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
2aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com/*
3aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * Copyright 2011 Google Inc.
4aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com *
5aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * Use of this source code is governed by a BSD-style license that can be
6aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com * found in the LICENSE file.
7aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com */
8aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
9aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
10aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com#include "GrRenderTarget.h"
11aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
12aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com#include "GrContext.h"
13aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com#include "GrGpu.h"
1481c3f8de1cbb93a8b99d730a75ab16d864612e95bsalomon@google.com#include "GrStencilBuffer.h"
15aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
16aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.combool GrRenderTarget::readPixels(int left, int top, int width, int height,
170342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                                GrPixelConfig config,
180342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                                void* buffer,
190342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                                size_t rowBytes,
200342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                                uint32_t pixelOpsFlags) {
21aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    // go through context so that all necessary flushing occurs
226f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com    GrContext* context = this->getContext();
236f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com    if (NULL == context) {
246f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com        return false;
256f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com    }
26aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    return context->readRenderTargetPixels(this,
270342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                                           left, top, width, height,
280342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                                           config, buffer, rowBytes,
290342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                                           pixelOpsFlags);
306f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com}
316f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com
326f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.comvoid GrRenderTarget::writePixels(int left, int top, int width, int height,
330342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                                 GrPixelConfig config,
340342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                                 const void* buffer,
350342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                                 size_t rowBytes,
360342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                                 uint32_t pixelOpsFlags) {
376f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com    // go through context so that all necessary flushing occurs
386f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com    GrContext* context = this->getContext();
396f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com    if (NULL == context) {
406f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com        return;
416f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com    }
426f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com    context->writeRenderTargetPixels(this,
430342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                                     left, top, width, height,
440342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                                     config, buffer, rowBytes,
450342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                                     pixelOpsFlags);
46aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com}
47aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
4875f9f25d8bf2adc0494f9afbbd5965809ee13acabsalomon@google.comvoid GrRenderTarget::resolve() {
4975f9f25d8bf2adc0494f9afbbd5965809ee13acabsalomon@google.com    // go through context so that all necessary flushing occurs
5075f9f25d8bf2adc0494f9afbbd5965809ee13acabsalomon@google.com    GrContext* context = this->getContext();
5175f9f25d8bf2adc0494f9afbbd5965809ee13acabsalomon@google.com    if (NULL == context) {
5275f9f25d8bf2adc0494f9afbbd5965809ee13acabsalomon@google.com        return;
5375f9f25d8bf2adc0494f9afbbd5965809ee13acabsalomon@google.com    }
5475f9f25d8bf2adc0494f9afbbd5965809ee13acabsalomon@google.com    context->resolveRenderTarget(this);
5575f9f25d8bf2adc0494f9afbbd5965809ee13acabsalomon@google.com}
5675f9f25d8bf2adc0494f9afbbd5965809ee13acabsalomon@google.com
5728361fad1054d59ed4e6a320c7a8b8782a1487c7commit-bot@chromium.orgvoid GrRenderTarget::discard() {
5828361fad1054d59ed4e6a320c7a8b8782a1487c7commit-bot@chromium.org    // go through context so that all necessary flushing occurs
5928361fad1054d59ed4e6a320c7a8b8782a1487c7commit-bot@chromium.org    GrContext* context = this->getContext();
6028361fad1054d59ed4e6a320c7a8b8782a1487c7commit-bot@chromium.org    if (NULL == context) {
6128361fad1054d59ed4e6a320c7a8b8782a1487c7commit-bot@chromium.org        return;
6228361fad1054d59ed4e6a320c7a8b8782a1487c7commit-bot@chromium.org    }
6328361fad1054d59ed4e6a320c7a8b8782a1487c7commit-bot@chromium.org    context->discardRenderTarget(this);
6428361fad1054d59ed4e6a320c7a8b8782a1487c7commit-bot@chromium.org}
6528361fad1054d59ed4e6a320c7a8b8782a1487c7commit-bot@chromium.org
66089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.orgsize_t GrRenderTarget::gpuMemorySize() const {
67adacc7067ad617cdc7bbef39192ca80f4b4d27f9robertphillips@google.com    size_t colorBits;
68e98ade4fdee03a234f68669bea84d07db4a8527erobertphillips@google.com    if (kUnknown_GrPixelConfig == fDesc.fConfig) {
69aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com        colorBits = 32; // don't know, make a guess
70aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    } else {
71e98ade4fdee03a234f68669bea84d07db4a8527erobertphillips@google.com        colorBits = GrBytesPerPixel(fDesc.fConfig);
72aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    }
73e98ade4fdee03a234f68669bea84d07db4a8527erobertphillips@google.com    uint64_t size = fDesc.fWidth;
74e98ade4fdee03a234f68669bea84d07db4a8527erobertphillips@google.com    size *= fDesc.fHeight;
75f6ff4951f7cf40fab078eb8016ceb7f53b8ec81ebsalomon@google.com    size *= colorBits;
76972f9cd7a063d0544f8c919fd12b9a3adbd12b24commit-bot@chromium.org    size *= SkTMax(1, fDesc.fSampleCnt);
77f6ff4951f7cf40fab078eb8016ceb7f53b8ec81ebsalomon@google.com    return (size_t)(size / 8);
78aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com}
79aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
80fd03d4a829efe2d77a712fd991927c55f59a2ffecommit-bot@chromium.orgvoid GrRenderTarget::flagAsNeedingResolve(const SkIRect* rect) {
81aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    if (kCanResolve_ResolveType == getResolveType()) {
82aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com        if (NULL != rect) {
83aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com            fResolveRect.join(*rect);
84aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com            if (!fResolveRect.intersect(0, 0, this->width(), this->height())) {
85aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com                fResolveRect.setEmpty();
86aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com            }
87aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com        } else {
88aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com            fResolveRect.setLTRB(0, 0, this->width(), this->height());
89aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com        }
90aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    }
91aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com}
92aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com
93fd03d4a829efe2d77a712fd991927c55f59a2ffecommit-bot@chromium.orgvoid GrRenderTarget::overrideResolveRect(const SkIRect rect) {
94aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    fResolveRect = rect;
95aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    if (fResolveRect.isEmpty()) {
96aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com        fResolveRect.setLargestInverted();
97aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    } else {
98aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com        if (!fResolveRect.intersect(0, 0, this->width(), this->height())) {
99aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com            fResolveRect.setLargestInverted();
100aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com        }
101aa5b6730f2658618015ed56ffb5882f15b6f053fbsalomon@google.com    }
10281c3f8de1cbb93a8b99d730a75ab16d864612e95bsalomon@google.com}
10381c3f8de1cbb93a8b99d730a75ab16d864612e95bsalomon@google.com
10481c3f8de1cbb93a8b99d730a75ab16d864612e95bsalomon@google.comvoid GrRenderTarget::setStencilBuffer(GrStencilBuffer* stencilBuffer) {
10576202b8dc39652b2832b279fc4cb798a0ea03ed3bsalomon@google.com    SkRefCnt_SafeAssign(fStencilBuffer, stencilBuffer);
106f6ff4951f7cf40fab078eb8016ceb7f53b8ec81ebsalomon@google.com}
107d6bbbf8a831cc982cda9b91e84c5600c631af5b2robertphillips@google.com
108d6bbbf8a831cc982cda9b91e84c5600c631af5b2robertphillips@google.comvoid GrRenderTarget::onRelease() {
109d6bbbf8a831cc982cda9b91e84c5600c631af5b2robertphillips@google.com    this->setStencilBuffer(NULL);
110d364554bcfd391c3b6111af8bff963a35ab87ba7robertphillips@google.com
111d364554bcfd391c3b6111af8bff963a35ab87ba7robertphillips@google.com    INHERITED::onRelease();
112d6bbbf8a831cc982cda9b91e84c5600c631af5b2robertphillips@google.com}
113d6bbbf8a831cc982cda9b91e84c5600c631af5b2robertphillips@google.com
114d6bbbf8a831cc982cda9b91e84c5600c631af5b2robertphillips@google.comvoid GrRenderTarget::onAbandon() {
115d6bbbf8a831cc982cda9b91e84c5600c631af5b2robertphillips@google.com    this->setStencilBuffer(NULL);
116d364554bcfd391c3b6111af8bff963a35ab87ba7robertphillips@google.com
117d364554bcfd391c3b6111af8bff963a35ab87ba7robertphillips@google.com    INHERITED::onAbandon();
118d6bbbf8a831cc982cda9b91e84c5600c631af5b2robertphillips@google.com}
119