Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(63)

Side by Side Diff: src/gpu/SkGrPixelRef.cpp

Issue 533323002: Use SkBitmapCache to optimize readPixels on a texture-backed bitmap (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Rebase master Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/core/SkBitmapDevice.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* 2 /*
3 * Copyright 2010 Google Inc. 3 * Copyright 2010 Google Inc.
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 8
9 9
10 10
11 #include "SkGrPixelRef.h" 11 #include "SkGrPixelRef.h"
12
12 #include "GrContext.h" 13 #include "GrContext.h"
13 #include "GrTexture.h" 14 #include "GrTexture.h"
15 #include "SkBitmapCache.h"
14 #include "SkGr.h" 16 #include "SkGr.h"
15 #include "SkRect.h" 17 #include "SkRect.h"
16 18
17 // since we call lockPixels recursively on fBitmap, we need a distinct mutex, 19 // since we call lockPixels recursively on fBitmap, we need a distinct mutex,
18 // to avoid deadlock with the default one provided by SkPixelRef. 20 // to avoid deadlock with the default one provided by SkPixelRef.
19 SK_DECLARE_STATIC_MUTEX(gROLockPixelsPixelRefMutex); 21 SK_DECLARE_STATIC_MUTEX(gROLockPixelsPixelRefMutex);
20 22
21 SkROLockPixelsPixelRef::SkROLockPixelsPixelRef(const SkImageInfo& info) 23 SkROLockPixelsPixelRef::SkROLockPixelsPixelRef(const SkImageInfo& info)
22 : INHERITED(info, &gROLockPixelsPixelRefMutex) {} 24 : INHERITED(info, &gROLockPixelsPixelRefMutex) {}
23 25
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 if (fSurface) { 138 if (fSurface) {
137 return fSurface->asTexture(); 139 return fSurface->asTexture();
138 } 140 }
139 return NULL; 141 return NULL;
140 } 142 }
141 143
142 SkPixelRef* SkGrPixelRef::deepCopy(SkColorType dstCT, const SkIRect* subset) { 144 SkPixelRef* SkGrPixelRef::deepCopy(SkColorType dstCT, const SkIRect* subset) {
143 if (NULL == fSurface) { 145 if (NULL == fSurface) {
144 return NULL; 146 return NULL;
145 } 147 }
146 148
147 // Note that when copying a render-target-backed pixel ref, we 149 // Note that when copying a render-target-backed pixel ref, we
148 // return a texture-backed pixel ref instead. This is because 150 // return a texture-backed pixel ref instead. This is because
149 // render-target pixel refs are usually created in conjunction with 151 // render-target pixel refs are usually created in conjunction with
150 // a GrTexture owned elsewhere (e.g., SkGpuDevice), and cannot live 152 // a GrTexture owned elsewhere (e.g., SkGpuDevice), and cannot live
151 // independently of that texture. Texture-backed pixel refs, on the other 153 // independently of that texture. Texture-backed pixel refs, on the other
152 // hand, own their GrTextures, and are thus self-contained. 154 // hand, own their GrTextures, and are thus self-contained.
153 return copyToTexturePixelRef(fSurface->asTexture(), dstCT, subset); 155 return copyToTexturePixelRef(fSurface->asTexture(), dstCT, subset);
154 } 156 }
155 157
158 static bool tryAllocBitmapPixels(SkBitmap* bitmap) {
159 SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator();
160 if (NULL != allocator) {
161 return allocator->allocPixelRef(bitmap, 0);
162 } else {
163 // DiscardableMemory is not available, fallback to default allocator
164 return bitmap->tryAllocPixels();
165 }
166 }
167
156 bool SkGrPixelRef::onReadPixels(SkBitmap* dst, const SkIRect* subset) { 168 bool SkGrPixelRef::onReadPixels(SkBitmap* dst, const SkIRect* subset) {
157 if (NULL == fSurface || fSurface->wasDestroyed()) { 169 if (NULL == fSurface || fSurface->wasDestroyed()) {
158 return false; 170 return false;
159 } 171 }
160 172
161 int left, top, width, height; 173 SkIRect bounds;
162 if (subset) { 174 if (subset) {
163 left = subset->fLeft; 175 bounds = *subset;
164 width = subset->width();
165 top = subset->fTop;
166 height = subset->height();
167 } else { 176 } else {
168 left = 0; 177 bounds = SkIRect::MakeWH(this->info().width(), this->info().height());
169 width = this->info().width();
170 top = 0;
171 height = this->info().height();
172 } 178 }
173 if (!dst->tryAllocN32Pixels(width, height)) { 179
174 SkDebugf("SkGrPixelRef::onReadPixels failed to alloc bitmap for result!\ n"); 180 //Check the cache
175 return false; 181 if(!SkBitmapCache::Find(this->getGenerationID(), bounds, dst)) {
182 //Cache miss
183
184 SkBitmap cachedBitmap;
185 cachedBitmap.setInfo(this->info().makeWH(bounds.width(), bounds.height() ));
186
187 // If we can't alloc the pixels, then fail
188 if (!tryAllocBitmapPixels(&cachedBitmap)) {
189 return false;
190 }
191
192 // Try to read the pixels from the surface
193 void* buffer = cachedBitmap.getPixels();
194 bool readPixelsOk = fSurface->readPixels(bounds.fLeft, bounds.fTop,
195 bounds.width(), bounds.height(),
196 kSkia8888_GrPixelConfig,
197 buffer, cachedBitmap.rowBytes());
198
199 if (!readPixelsOk) {
200 return false;
201 }
202
203 // If we are here, pixels were read correctly from the surface.
204 cachedBitmap.setImmutable();
205 //Add to the cache
206 SkBitmapCache::Add(this->getGenerationID(), bounds, cachedBitmap);
207
208 dst->swap(cachedBitmap);
176 } 209 }
177 SkAutoLockPixels al(*dst); 210
178 void* buffer = dst->getPixels(); 211 return true;
179 return fSurface->readPixels(left, top, width, height, 212
180 kSkia8888_GrPixelConfig,
181 buffer, dst->rowBytes());
182 } 213 }
OLDNEW
« no previous file with comments | « src/core/SkBitmapDevice.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698