blob: 8a16437780e3183b77cc0a6c849c2d119a256437 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2010 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.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
robertphillips@google.com5088eb42012-06-28 20:59:13 +000011#include "SkGrPixelRef.h"
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000012#include "GrContext.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000013#include "GrTexture.h"
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000014#include "SkGr.h"
bsalomon@google.com669fdc42011-04-05 17:08:27 +000015#include "SkRect.h"
reed@google.com9c49bc32011-07-07 13:42:37 +000016
17// since we call lockPixels recursively on fBitmap, we need a distinct mutex,
18// to avoid deadlock with the default one provided by SkPixelRef.
digit@google.com1771cbf2012-01-26 21:26:40 +000019SK_DECLARE_STATIC_MUTEX(gROLockPixelsPixelRefMutex);
reed@google.com9c49bc32011-07-07 13:42:37 +000020
reed@google.combf790232013-12-13 19:45:58 +000021SkROLockPixelsPixelRef::SkROLockPixelsPixelRef(const SkImageInfo& info)
22 : INHERITED(info, &gROLockPixelsPixelRefMutex) {}
reed@google.com9c49bc32011-07-07 13:42:37 +000023
reed@google.combf790232013-12-13 19:45:58 +000024SkROLockPixelsPixelRef::~SkROLockPixelsPixelRef() {}
reed@google.com9c49bc32011-07-07 13:42:37 +000025
reed@google.comd0419b12014-01-06 17:08:27 +000026bool SkROLockPixelsPixelRef::onNewLockPixels(LockRec* rec) {
reed@google.com9c49bc32011-07-07 13:42:37 +000027 fBitmap.reset();
28// SkDebugf("---------- calling readpixels in support of lockpixels\n");
29 if (!this->onReadPixels(&fBitmap, NULL)) {
30 SkDebugf("SkROLockPixelsPixelRef::onLockPixels failed!\n");
reed@google.comd0419b12014-01-06 17:08:27 +000031 return false;
reed@google.com9c49bc32011-07-07 13:42:37 +000032 }
33 fBitmap.lockPixels();
reed@google.comd0419b12014-01-06 17:08:27 +000034 if (NULL == fBitmap.getPixels()) {
35 return false;
36 }
37
38 rec->fPixels = fBitmap.getPixels();
39 rec->fColorTable = NULL;
40 rec->fRowBytes = fBitmap.rowBytes();
41 return true;
reed@google.com9c49bc32011-07-07 13:42:37 +000042}
43
44void SkROLockPixelsPixelRef::onUnlockPixels() {
45 fBitmap.unlockPixels();
46}
47
48bool SkROLockPixelsPixelRef::onLockPixelsAreWritable() const {
49 return false;
50}
51
52///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com669fdc42011-04-05 17:08:27 +000053
scroggo@google.coma2a31922012-12-07 19:14:45 +000054static SkGrPixelRef* copyToTexturePixelRef(GrTexture* texture, SkBitmap::Config dstConfig,
55 const SkIRect* subset) {
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000056 if (NULL == texture) {
57 return NULL;
58 }
59 GrContext* context = texture->getContext();
60 if (NULL == context) {
61 return NULL;
62 }
63 GrTextureDesc desc;
64
scroggo@google.coma2a31922012-12-07 19:14:45 +000065 SkIPoint pointStorage;
66 SkIPoint* topLeft;
67 if (subset != NULL) {
68 SkASSERT(SkIRect::MakeWH(texture->width(), texture->height()).contains(*subset));
69 // Create a new texture that is the size of subset.
70 desc.fWidth = subset->width();
71 desc.fHeight = subset->height();
72 pointStorage.set(subset->x(), subset->y());
73 topLeft = &pointStorage;
74 } else {
75 desc.fWidth = texture->width();
76 desc.fHeight = texture->height();
77 topLeft = NULL;
78 }
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000079 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
rileya@google.com24f3ad12012-07-18 21:47:40 +000080 desc.fConfig = SkBitmapConfig2GrPixelConfig(dstConfig);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000081
reed@google.combf790232013-12-13 19:45:58 +000082 SkImageInfo info;
83 if (!GrPixelConfig2ColorType(desc.fConfig, &info.fColorType)) {
84 return NULL;
85 }
86 info.fWidth = desc.fWidth;
87 info.fHeight = desc.fHeight;
88 info.fAlphaType = kPremul_SkAlphaType;
skia.committer@gmail.com96f5fa02013-12-16 07:01:40 +000089
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000090 GrTexture* dst = context->createUncachedTexture(desc, NULL, 0);
91 if (NULL == dst) {
92 return NULL;
93 }
94
scroggo@google.coma2a31922012-12-07 19:14:45 +000095 context->copyTexture(texture, dst->asRenderTarget(), topLeft);
robertphillips@google.comd881bc12012-06-28 20:02:39 +000096
robertphillips@google.com41efe042012-06-29 20:55:14 +000097 // TODO: figure out if this is responsible for Chrome canvas errors
98#if 0
robertphillips@google.comd881bc12012-06-28 20:02:39 +000099 // The render texture we have created (to perform the copy) isn't fully
100 // functional (since it doesn't have a stencil buffer). Release it here
101 // so the caller doesn't try to render to it.
102 // TODO: we can undo this release when dynamic stencil buffer attach/
103 // detach has been implemented
104 dst->releaseRenderTarget();
robertphillips@google.com41efe042012-06-29 20:55:14 +0000105#endif
robertphillips@google.comd881bc12012-06-28 20:02:39 +0000106
reed@google.combf790232013-12-13 19:45:58 +0000107 SkGrPixelRef* pixelRef = SkNEW_ARGS(SkGrPixelRef, (info, dst));
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000108 SkSafeUnref(dst);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000109 return pixelRef;
110}
111
112///////////////////////////////////////////////////////////////////////////////
113
reed@google.combf790232013-12-13 19:45:58 +0000114SkGrPixelRef::SkGrPixelRef(const SkImageInfo& info, GrSurface* surface,
115 bool transferCacheLock) : INHERITED(info) {
reed@google.com61867872013-12-09 13:41:06 +0000116 // TODO: figure out if this is responsible for Chrome canvas errors
117#if 0
118 // The GrTexture has a ref to the GrRenderTarget but not vice versa.
119 // If the GrTexture exists take a ref to that (rather than the render
120 // target)
121 fSurface = surface->asTexture();
122#else
123 fSurface = NULL;
124#endif
125 if (NULL == fSurface) {
126 fSurface = surface;
127 }
128 fUnlock = transferCacheLock;
129 SkSafeRef(surface);
130}
131
robertphillips@google.comd881bc12012-06-28 20:02:39 +0000132SkGrPixelRef::~SkGrPixelRef() {
bsalomon@google.com8090e652012-08-28 15:07:11 +0000133 if (fUnlock) {
134 GrContext* context = fSurface->getContext();
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000135 GrTexture* texture = fSurface->asTexture();
bsalomon@google.com8090e652012-08-28 15:07:11 +0000136 if (NULL != context && NULL != texture) {
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000137 context->unlockScratchTexture(texture);
bsalomon@google.com8090e652012-08-28 15:07:11 +0000138 }
139 }
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000140 SkSafeUnref(fSurface);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000141}
142
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +0000143GrTexture* SkGrPixelRef::getTexture() {
robertphillips@google.comd881bc12012-06-28 20:02:39 +0000144 if (NULL != fSurface) {
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +0000145 return fSurface->asTexture();
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000146 }
147 return NULL;
148}
149
scroggo@google.coma2a31922012-12-07 19:14:45 +0000150SkPixelRef* SkGrPixelRef::deepCopy(SkBitmap::Config dstConfig, const SkIRect* subset) {
robertphillips@google.comd881bc12012-06-28 20:02:39 +0000151 if (NULL == fSurface) {
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000152 return NULL;
153 }
robertphillips@google.comd881bc12012-06-28 20:02:39 +0000154
155 // Note that when copying a render-target-backed pixel ref, we
156 // return a texture-backed pixel ref instead. This is because
157 // render-target pixel refs are usually created in conjunction with
158 // a GrTexture owned elsewhere (e.g., SkGpuDevice), and cannot live
159 // independently of that texture. Texture-backed pixel refs, on the other
160 // hand, own their GrTextures, and are thus self-contained.
scroggo@google.coma2a31922012-12-07 19:14:45 +0000161 return copyToTexturePixelRef(fSurface->asTexture(), dstConfig, subset);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000162}
163
robertphillips@google.comd881bc12012-06-28 20:02:39 +0000164bool SkGrPixelRef::onReadPixels(SkBitmap* dst, const SkIRect* subset) {
165 if (NULL == fSurface || !fSurface->isValid()) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000166 return false;
167 }
robertphillips@google.comd881bc12012-06-28 20:02:39 +0000168
169 int left, top, width, height;
170 if (NULL != subset) {
171 left = subset->fLeft;
172 width = subset->width();
173 top = subset->fTop;
174 height = subset->height();
175 } else {
176 left = 0;
177 width = fSurface->width();
178 top = 0;
179 height = fSurface->height();
180 }
181 dst->setConfig(SkBitmap::kARGB_8888_Config, width, height);
bsalomon@google.com009bcca2012-12-18 21:40:08 +0000182 if (!dst->allocPixels()) {
183 SkDebugf("SkGrPixelRef::onReadPixels failed to alloc bitmap for result!\n");
184 return false;
185 }
robertphillips@google.comd881bc12012-06-28 20:02:39 +0000186 SkAutoLockPixels al(*dst);
187 void* buffer = dst->getPixels();
188 return fSurface->readPixels(left, top, width, height,
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000189 kSkia8888_GrPixelConfig,
robertphillips@google.comd881bc12012-06-28 20:02:39 +0000190 buffer, dst->rowBytes());
reed@google.com4281d652011-04-08 18:54:20 +0000191}