gfx/skia/trunk/src/gpu/SkGrPixelRef.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1
michael@0 2 /*
michael@0 3 * Copyright 2010 Google Inc.
michael@0 4 *
michael@0 5 * Use of this source code is governed by a BSD-style license that can be
michael@0 6 * found in the LICENSE file.
michael@0 7 */
michael@0 8
michael@0 9
michael@0 10
michael@0 11 #include "SkGrPixelRef.h"
michael@0 12 #include "GrContext.h"
michael@0 13 #include "GrTexture.h"
michael@0 14 #include "SkGr.h"
michael@0 15 #include "SkRect.h"
michael@0 16
michael@0 17 // since we call lockPixels recursively on fBitmap, we need a distinct mutex,
michael@0 18 // to avoid deadlock with the default one provided by SkPixelRef.
michael@0 19 SK_DECLARE_STATIC_MUTEX(gROLockPixelsPixelRefMutex);
michael@0 20
michael@0 21 SkROLockPixelsPixelRef::SkROLockPixelsPixelRef(const SkImageInfo& info)
michael@0 22 : INHERITED(info, &gROLockPixelsPixelRefMutex) {}
michael@0 23
michael@0 24 SkROLockPixelsPixelRef::~SkROLockPixelsPixelRef() {}
michael@0 25
michael@0 26 bool SkROLockPixelsPixelRef::onNewLockPixels(LockRec* rec) {
michael@0 27 fBitmap.reset();
michael@0 28 // SkDebugf("---------- calling readpixels in support of lockpixels\n");
michael@0 29 if (!this->onReadPixels(&fBitmap, NULL)) {
michael@0 30 SkDebugf("SkROLockPixelsPixelRef::onLockPixels failed!\n");
michael@0 31 return false;
michael@0 32 }
michael@0 33 fBitmap.lockPixels();
michael@0 34 if (NULL == fBitmap.getPixels()) {
michael@0 35 return false;
michael@0 36 }
michael@0 37
michael@0 38 rec->fPixels = fBitmap.getPixels();
michael@0 39 rec->fColorTable = NULL;
michael@0 40 rec->fRowBytes = fBitmap.rowBytes();
michael@0 41 return true;
michael@0 42 }
michael@0 43
michael@0 44 void SkROLockPixelsPixelRef::onUnlockPixels() {
michael@0 45 fBitmap.unlockPixels();
michael@0 46 }
michael@0 47
michael@0 48 bool SkROLockPixelsPixelRef::onLockPixelsAreWritable() const {
michael@0 49 return false;
michael@0 50 }
michael@0 51
michael@0 52 ///////////////////////////////////////////////////////////////////////////////
michael@0 53
michael@0 54 static SkGrPixelRef* copyToTexturePixelRef(GrTexture* texture, SkBitmap::Config dstConfig,
michael@0 55 const SkIRect* subset) {
michael@0 56 if (NULL == texture) {
michael@0 57 return NULL;
michael@0 58 }
michael@0 59 GrContext* context = texture->getContext();
michael@0 60 if (NULL == context) {
michael@0 61 return NULL;
michael@0 62 }
michael@0 63 GrTextureDesc desc;
michael@0 64
michael@0 65 SkIPoint pointStorage;
michael@0 66 SkIPoint* topLeft;
michael@0 67 if (subset != NULL) {
michael@0 68 SkASSERT(SkIRect::MakeWH(texture->width(), texture->height()).contains(*subset));
michael@0 69 // Create a new texture that is the size of subset.
michael@0 70 desc.fWidth = subset->width();
michael@0 71 desc.fHeight = subset->height();
michael@0 72 pointStorage.set(subset->x(), subset->y());
michael@0 73 topLeft = &pointStorage;
michael@0 74 } else {
michael@0 75 desc.fWidth = texture->width();
michael@0 76 desc.fHeight = texture->height();
michael@0 77 topLeft = NULL;
michael@0 78 }
michael@0 79 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
michael@0 80 desc.fConfig = SkBitmapConfig2GrPixelConfig(dstConfig);
michael@0 81
michael@0 82 SkImageInfo info;
michael@0 83 if (!GrPixelConfig2ColorType(desc.fConfig, &info.fColorType)) {
michael@0 84 return NULL;
michael@0 85 }
michael@0 86 info.fWidth = desc.fWidth;
michael@0 87 info.fHeight = desc.fHeight;
michael@0 88 info.fAlphaType = kPremul_SkAlphaType;
michael@0 89
michael@0 90 GrTexture* dst = context->createUncachedTexture(desc, NULL, 0);
michael@0 91 if (NULL == dst) {
michael@0 92 return NULL;
michael@0 93 }
michael@0 94
michael@0 95 context->copyTexture(texture, dst->asRenderTarget(), topLeft);
michael@0 96
michael@0 97 // TODO: figure out if this is responsible for Chrome canvas errors
michael@0 98 #if 0
michael@0 99 // The render texture we have created (to perform the copy) isn't fully
michael@0 100 // functional (since it doesn't have a stencil buffer). Release it here
michael@0 101 // so the caller doesn't try to render to it.
michael@0 102 // TODO: we can undo this release when dynamic stencil buffer attach/
michael@0 103 // detach has been implemented
michael@0 104 dst->releaseRenderTarget();
michael@0 105 #endif
michael@0 106
michael@0 107 SkGrPixelRef* pixelRef = SkNEW_ARGS(SkGrPixelRef, (info, dst));
michael@0 108 SkSafeUnref(dst);
michael@0 109 return pixelRef;
michael@0 110 }
michael@0 111
michael@0 112 ///////////////////////////////////////////////////////////////////////////////
michael@0 113
michael@0 114 SkGrPixelRef::SkGrPixelRef(const SkImageInfo& info, GrSurface* surface,
michael@0 115 bool transferCacheLock) : INHERITED(info) {
michael@0 116 // TODO: figure out if this is responsible for Chrome canvas errors
michael@0 117 #if 0
michael@0 118 // The GrTexture has a ref to the GrRenderTarget but not vice versa.
michael@0 119 // If the GrTexture exists take a ref to that (rather than the render
michael@0 120 // target)
michael@0 121 fSurface = surface->asTexture();
michael@0 122 #else
michael@0 123 fSurface = NULL;
michael@0 124 #endif
michael@0 125 if (NULL == fSurface) {
michael@0 126 fSurface = surface;
michael@0 127 }
michael@0 128 fUnlock = transferCacheLock;
michael@0 129 SkSafeRef(surface);
michael@0 130
michael@0 131 if (fSurface) {
michael@0 132 SkASSERT(info.fWidth <= fSurface->width());
michael@0 133 SkASSERT(info.fHeight <= fSurface->height());
michael@0 134 }
michael@0 135 }
michael@0 136
michael@0 137 SkGrPixelRef::~SkGrPixelRef() {
michael@0 138 if (fUnlock) {
michael@0 139 GrContext* context = fSurface->getContext();
michael@0 140 GrTexture* texture = fSurface->asTexture();
michael@0 141 if (NULL != context && NULL != texture) {
michael@0 142 context->unlockScratchTexture(texture);
michael@0 143 }
michael@0 144 }
michael@0 145 SkSafeUnref(fSurface);
michael@0 146 }
michael@0 147
michael@0 148 GrTexture* SkGrPixelRef::getTexture() {
michael@0 149 if (NULL != fSurface) {
michael@0 150 return fSurface->asTexture();
michael@0 151 }
michael@0 152 return NULL;
michael@0 153 }
michael@0 154
michael@0 155 SkPixelRef* SkGrPixelRef::deepCopy(SkBitmap::Config dstConfig, const SkIRect* subset) {
michael@0 156 if (NULL == fSurface) {
michael@0 157 return NULL;
michael@0 158 }
michael@0 159
michael@0 160 // Note that when copying a render-target-backed pixel ref, we
michael@0 161 // return a texture-backed pixel ref instead. This is because
michael@0 162 // render-target pixel refs are usually created in conjunction with
michael@0 163 // a GrTexture owned elsewhere (e.g., SkGpuDevice), and cannot live
michael@0 164 // independently of that texture. Texture-backed pixel refs, on the other
michael@0 165 // hand, own their GrTextures, and are thus self-contained.
michael@0 166 return copyToTexturePixelRef(fSurface->asTexture(), dstConfig, subset);
michael@0 167 }
michael@0 168
michael@0 169 bool SkGrPixelRef::onReadPixels(SkBitmap* dst, const SkIRect* subset) {
michael@0 170 if (NULL == fSurface || !fSurface->isValid()) {
michael@0 171 return false;
michael@0 172 }
michael@0 173
michael@0 174 int left, top, width, height;
michael@0 175 if (NULL != subset) {
michael@0 176 left = subset->fLeft;
michael@0 177 width = subset->width();
michael@0 178 top = subset->fTop;
michael@0 179 height = subset->height();
michael@0 180 } else {
michael@0 181 left = 0;
michael@0 182 width = this->info().fWidth;
michael@0 183 top = 0;
michael@0 184 height = this->info().fHeight;
michael@0 185 }
michael@0 186 if (!dst->allocPixels(SkImageInfo::MakeN32Premul(width, height))) {
michael@0 187 SkDebugf("SkGrPixelRef::onReadPixels failed to alloc bitmap for result!\n");
michael@0 188 return false;
michael@0 189 }
michael@0 190 SkAutoLockPixels al(*dst);
michael@0 191 void* buffer = dst->getPixels();
michael@0 192 return fSurface->readPixels(left, top, width, height,
michael@0 193 kSkia8888_GrPixelConfig,
michael@0 194 buffer, dst->rowBytes());
michael@0 195 }

mercurial