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

Side by Side Diff: src/core/SkImageCacherator.cpp

Issue 1249543003: Creating functions for uploading a mipmapped texture. (Closed) Base URL: https://chromium.googlesource.com/skia.git@master
Patch Set: Fixing merge mistakes Created 5 years, 2 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
OLDNEW
1 /* 1 /*
2 * Copyright 2015 Google Inc. 2 * Copyright 2015 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkBitmap.h" 8 #include "SkBitmap.h"
9 #include "SkBitmapCache.h" 9 #include "SkBitmapCache.h"
10 #include "SkImage_Base.h" 10 #include "SkImage_Base.h"
11 #include "SkImageCacherator.h" 11 #include "SkImageCacherator.h"
12 #include "SkMallocPixelRef.h" 12 #include "SkMallocPixelRef.h"
13 #include "SkNextID.h" 13 #include "SkNextID.h"
14 #include "SkPixelRef.h" 14 #include "SkPixelRef.h"
15 #include "SkResourceCache.h" 15 #include "SkResourceCache.h"
16 16
17 #if SK_SUPPORT_GPU 17 #if SK_SUPPORT_GPU
18 #include "GrContext.h" 18 #include "GrContext.h"
19 #include "GrGpuResourcePriv.h" 19 #include "GrGpuResourcePriv.h"
20 #include "GrResourceKey.h" 20 #include "GrResourceKey.h"
21 #include "GrTextureAccess.h" 21 #include "GrTextureParams.h"
22 #include "GrYUVProvider.h" 22 #include "GrYUVProvider.h"
23 #include "SkGr.h" 23 #include "SkGr.h"
24 #include "SkGrPriv.h" 24 #include "SkGrPriv.h"
25 #endif 25 #endif
26 26
27 SkImageCacherator* SkImageCacherator::NewFromGenerator(SkImageGenerator* gen, 27 SkImageCacherator* SkImageCacherator::NewFromGenerator(SkImageGenerator* gen,
28 const SkIRect* subset) { 28 const SkIRect* subset) {
29 if (!gen) { 29 if (!gen) {
30 return nullptr; 30 return nullptr;
31 } 31 }
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 return check_output_bitmap(*bitmap, fUniqueID); 133 return check_output_bitmap(*bitmap, fUniqueID);
134 } 134 }
135 135
136 #if SK_SUPPORT_GPU 136 #if SK_SUPPORT_GPU
137 // Try to get a texture and read it back to raster (and then cache that with our ID) 137 // Try to get a texture and read it back to raster (and then cache that with our ID)
138 SkAutoTUnref<GrTexture> tex; 138 SkAutoTUnref<GrTexture> tex;
139 139
140 { 140 {
141 ScopedGenerator generator(this); 141 ScopedGenerator generator(this);
142 SkIRect subset = SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), fInfo.width (), fInfo.height()); 142 SkIRect subset = SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), fInfo.width (), fInfo.height());
143 tex.reset(generator->generateTexture(nullptr, kUntiled_SkImageUsageType, &subset)); 143 tex.reset(generator->generateTexture(nullptr, &subset));
144 } 144 }
145 if (!tex) { 145 if (!tex) {
146 bitmap->reset(); 146 bitmap->reset();
147 return false; 147 return false;
148 } 148 }
149 149
150 if (!bitmap->tryAllocPixels(fInfo)) { 150 if (!bitmap->tryAllocPixels(fInfo)) {
151 bitmap->reset(); 151 bitmap->reset();
152 return false; 152 return false;
153 } 153 }
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
200 bool onGetYUVPlanes(SkISize sizes[3], void* planes[3], size_t rowBytes[3], 200 bool onGetYUVPlanes(SkISize sizes[3], void* planes[3], size_t rowBytes[3],
201 SkYUVColorSpace* space) override { 201 SkYUVColorSpace* space) override {
202 return fGen->getYUV8Planes(sizes, planes, rowBytes, space); 202 return fGen->getYUV8Planes(sizes, planes, rowBytes, space);
203 } 203 }
204 }; 204 };
205 205
206 static GrTexture* set_key_and_return(GrTexture* tex, const GrUniqueKey& key) { 206 static GrTexture* set_key_and_return(GrTexture* tex, const GrUniqueKey& key) {
207 tex->resourcePriv().setUniqueKey(key); 207 tex->resourcePriv().setUniqueKey(key);
208 return tex; 208 return tex;
209 } 209 }
210 #endif
211 210
212 /* 211 /*
213 * We have a 5 ways to try to return a texture (in sorted order) 212 * We have a 5 ways to try to return a texture (in sorted order)
214 * 213 *
215 * 1. Check the cache for a pre-existing one 214 * 1. Check the cache for a pre-existing one
216 * 2. Ask the genreator to natively create one 215 * 2. Ask the generator to natively create one
217 * 3. Ask the generator to return a compressed form that the GPU might support 216 * 3. Ask the generator to return a compressed form that the GPU might support
218 * 4. Ask the generator to return YUV planes, which the GPU can convert 217 * 4. Ask the generator to return YUV planes, which the GPU can convert
219 * 5. Ask the generator to return RGB(A) data, which the GPU can convert 218 * 5. Ask the generator to return RGB(A) data, which the GPU can convert
220 */ 219 */
221 GrTexture* SkImageCacherator::lockAsTexture(GrContext* ctx, SkImageUsageType usa ge, 220 GrTexture* SkImageCacherator::lockUnstretchedTexture(GrContext* ctx, const SkIma ge* client) {
222 const SkImage* client) {
223 #if SK_SUPPORT_GPU
224 if (!ctx) {
225 return nullptr;
226 }
227
228 // textures (at least the texture-key) only support 16bit dimensions, so abo rt early 221 // textures (at least the texture-key) only support 16bit dimensions, so abo rt early
229 // if we're too big. 222 // if we're too big.
230 if (fInfo.width() > 0xFFFF || fInfo.height() > 0xFFFF) { 223 if (fInfo.width() > 0xFFFF || fInfo.height() > 0xFFFF) {
231 return nullptr; 224 return nullptr;
232 } 225 }
233 226
234 GrUniqueKey key; 227 GrUniqueKey key;
228 const GrTextureParams& noStretchParams = GrTextureParams::ClampNoFilter();
235 GrMakeKeyFromImageID(&key, fUniqueID, SkIRect::MakeWH(fInfo.width(), fInfo.h eight()), 229 GrMakeKeyFromImageID(&key, fUniqueID, SkIRect::MakeWH(fInfo.width(), fInfo.h eight()),
236 *ctx->caps(), usage); 230 *ctx->caps(), noStretchParams);
237 231
238 // 1. Check the cache for a pre-existing one 232 // 1. Check the cache for a pre-existing one
239 if (GrTexture* tex = ctx->textureProvider()->findAndRefTextureByUniqueKey(ke y)) { 233 if (GrTexture* tex = ctx->textureProvider()->findAndRefTextureByUniqueKey(ke y)) {
240 return tex; 234 return tex;
241 } 235 }
242 236
243 // 2. Ask the genreator to natively create one 237 // 2. Ask the genreator to natively create one
244 { 238 {
245 ScopedGenerator generator(this); 239 ScopedGenerator generator(this);
246 SkIRect subset = SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), fInfo.width (), fInfo.height()); 240 SkIRect subset = SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), fInfo.width (), fInfo.height());
247 if (GrTexture* tex = generator->generateTexture(ctx, usage, &subset)) { 241 if (GrTexture* tex = generator->generateTexture(ctx, &subset)) {
248 return set_key_and_return(tex, key); 242 return set_key_and_return(tex, key);
249 } 243 }
250 } 244 }
251 245
252 const GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(fInfo); 246 const GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(fInfo);
253 247
254 // 3. Ask the generator to return a compressed form that the GPU might suppo rt 248 // 3. Ask the generator to return a compressed form that the GPU might suppo rt
255 SkAutoTUnref<SkData> data(this->refEncoded()); 249 SkAutoTUnref<SkData> data(this->refEncoded());
256 if (data) { 250 if (data) {
257 GrTexture* tex = load_compressed_into_texture(ctx, data, desc); 251 GrTexture* tex = load_compressed_into_texture(ctx, data, desc);
258 if (tex) { 252 if (tex) {
259 return set_key_and_return(tex, key); 253 return set_key_and_return(tex, key);
260 } 254 }
261 } 255 }
262 256
263 // 4. Ask the generator to return YUV planes, which the GPU can convert 257 // 4. Ask the generator to return YUV planes, which the GPU can convert
264 { 258 {
265 ScopedGenerator generator(this); 259 ScopedGenerator generator(this);
266 Generator_GrYUVProvider provider(generator); 260 Generator_GrYUVProvider provider(generator);
267 GrTexture* tex = provider.refAsTexture(ctx, desc, true); 261 GrTexture* tex = provider.refAsTexture(ctx, desc, true);
268 if (tex) { 262 if (tex) {
269 return set_key_and_return(tex, key); 263 return set_key_and_return(tex, key);
270 } 264 }
271 } 265 }
272 266
273 // 5. Ask the generator to return RGB(A) data, which the GPU can convert 267 // 5. Ask the generator to return RGB(A) data, which the GPU can convert
274 SkBitmap bitmap; 268 SkBitmap bitmap;
275 if (this->tryLockAsBitmap(&bitmap, client)) { 269 if (this->tryLockAsBitmap(&bitmap, client)) {
276 return GrRefCachedBitmapTexture(ctx, bitmap, usage); 270 return GrRefCachedBitmapTexture(ctx, bitmap, noStretchParams);
277 } 271 }
278 #endif
279
280 return nullptr; 272 return nullptr;
281 } 273 }
282 274
275 //////////////////////////////////////////////////////////////////////////////// ///////////////////
276
277 #include "GrTextureMaker.h"
278
279 class Cacherator_GrTextureMaker : public GrTextureMaker {
280 public:
281 Cacherator_GrTextureMaker(SkImageCacherator* cacher, const GrTextureParams& params,
282 const SkImage* client, const GrUniqueKey& unstretc hedKey)
283 : INHERITED(cacher->info().width(), cacher->info().height())
284 , fCacher(cacher)
285 , fParams(params)
286 , fClient(client)
287 , fUnstretchedKey(unstretchedKey)
288 {}
289
290 protected:
291 // TODO: consider overriding this, for the case where the underlying generat or might be
292 // able to efficiently produce a "stretched" texture natively (e.g. pi cture-backed)
293 // GrTexture* onGenerateStretchedTexture(GrContext*, const SkGrStretch&) over ride;
294
295 GrTexture* onRefUnstretchedTexture(GrContext* ctx, const GrTextureParams*) o verride {
296 return fCacher->lockUnstretchedTexture(ctx, fClient);
297 }
298
299 bool onMakeStretchedKey(const SkGrStretch& stretch, GrUniqueKey* stretchedKe y) override {
300 return GrMakeStretchedKey(fUnstretchedKey, stretch, stretchedKey);
301 }
302
303 void onNotifyStretchCached(const GrUniqueKey& stretchedKey) override {
304 if (fClient) {
305 as_IB(fClient)->notifyAddedToCache();
306 }
307 }
308
309 bool onGetROBitmap(SkBitmap* bitmap) override {
310 return fCacher->lockAsBitmap(bitmap, fClient);
311 }
312
313 private:
314 SkImageCacherator* fCacher;
315 const GrTextureParams& fParams;
316 const SkImage* fClient;
317 const GrUniqueKey fUnstretchedKey;
318
319 typedef GrTextureMaker INHERITED;
320 };
321
322 GrTexture* SkImageCacherator::lockAsTexture(GrContext* ctx, const GrTextureParam s& params,
323 const SkImage* client) {
324 if (!ctx) {
325 return nullptr;
326 }
327
328 GrUniqueKey key;
329 GrMakeKeyFromImageID(&key, this->uniqueID(),
330 SkIRect::MakeWH(this->info().width(), this->info().heig ht()),
331 *ctx->caps(), GrTextureParams::ClampNoFilter());
332
333 return Cacherator_GrTextureMaker(this, params, client, key).refCachedTexture (ctx, params);
334 }
335
336 #else
337
338 GrTexture* SkImageCacherator::lockAsTexture(GrContext* ctx, const GrTextureParam s&,
339 const SkImage* client) {
340 return nullptr;
341 }
342
343 #endif
OLDNEW
« include/gpu/GrContext.h ('K') | « include/gpu/SkGr.h ('k') | src/core/SkMipMap.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698