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

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

Issue 1249543003: Creating functions for uploading a mipmapped texture. (Closed) Base URL: https://chromium.googlesource.com/skia.git@master
Patch Set: Moving glTexStorage over to a separate CL. Created 4 years, 11 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 2010 Google Inc. 2 * Copyright 2010 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 8
9 #include "SkGr.h" 9 #include "SkGr.h"
10 10
11 #include "GrCaps.h" 11 #include "GrCaps.h"
12 #include "GrContext.h" 12 #include "GrContext.h"
13 #include "GrTextureParamsAdjuster.h" 13 #include "GrTextureParamsAdjuster.h"
14 #include "GrGpuResourcePriv.h" 14 #include "GrGpuResourcePriv.h"
15 #include "GrImageIDTextureAdjuster.h" 15 #include "GrImageIDTextureAdjuster.h"
16 #include "GrXferProcessor.h" 16 #include "GrXferProcessor.h"
17 #include "GrYUVProvider.h" 17 #include "GrYUVProvider.h"
18 18
19 #include "SkColorFilter.h" 19 #include "SkColorFilter.h"
20 #include "SkConfig8888.h" 20 #include "SkConfig8888.h"
21 #include "SkCanvas.h" 21 #include "SkCanvas.h"
22 #include "SkData.h" 22 #include "SkData.h"
23 #include "SkErrorInternals.h" 23 #include "SkErrorInternals.h"
24 #include "SkGrPixelRef.h" 24 #include "SkGrPixelRef.h"
25 #include "SkMessageBus.h" 25 #include "SkMessageBus.h"
26 #include "SkMath.h"
27 #include "SkMipMapLevel.h"
26 #include "SkPixelRef.h" 28 #include "SkPixelRef.h"
27 #include "SkResourceCache.h" 29 #include "SkResourceCache.h"
28 #include "SkTextureCompressor.h" 30 #include "SkTextureCompressor.h"
31 #include "SkTypes.h"
29 #include "SkYUVPlanesCache.h" 32 #include "SkYUVPlanesCache.h"
30 #include "effects/GrBicubicEffect.h" 33 #include "effects/GrBicubicEffect.h"
31 #include "effects/GrConstColorProcessor.h" 34 #include "effects/GrConstColorProcessor.h"
32 #include "effects/GrDitherEffect.h" 35 #include "effects/GrDitherEffect.h"
33 #include "effects/GrPorterDuffXferProcessor.h" 36 #include "effects/GrPorterDuffXferProcessor.h"
34 #include "effects/GrXfermodeFragmentProcessor.h" 37 #include "effects/GrXfermodeFragmentProcessor.h"
35 #include "effects/GrYUVtoRGBEffect.h" 38 #include "effects/GrYUVtoRGBEffect.h"
36 39
37 #ifndef SK_IGNORE_ETC1_SUPPORT 40 #ifndef SK_IGNORE_ETC1_SUPPORT
38 # include "ktx.h" 41 # include "ktx.h"
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after
278 explicit Invalidator(const GrUniqueKey& key) : fMsg(key) {} 281 explicit Invalidator(const GrUniqueKey& key) : fMsg(key) {}
279 private: 282 private:
280 GrUniqueKeyInvalidatedMessage fMsg; 283 GrUniqueKeyInvalidatedMessage fMsg;
281 284
282 void onChange() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>:: Post(fMsg); } 285 void onChange() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>:: Post(fMsg); }
283 }; 286 };
284 287
285 pixelRef->addGenIDChangeListener(new Invalidator(key)); 288 pixelRef->addGenIDChangeListener(new Invalidator(key));
286 } 289 }
287 290
291 GrTexture* GrGenerateMipMapsAndUploadToTexture(GrContext* ctx, const SkBitmap& b itmap)
292 {
293 GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info());
294 if (kIndex_8_SkColorType != bitmap.colorType() && !bitmap.readyToDraw()) {
295 GrTexture *texture = load_etc1_texture(ctx, bitmap, desc);
296 if (texture) {
297 return texture;
298 }
299 }
300
301 GrTexture *texture = create_texture_from_yuv(ctx, bitmap, desc);
302 if (texture) {
303 return texture;
304 }
305
306 SkASSERT(sizeof(int) <= sizeof(uint32_t));
307 if (bitmap.width() < 0 || bitmap.height() < 0) {
308 return nullptr;
309 }
310 const uint32_t baseWidth = static_cast<uint32_t>(bitmap.width());
311 const uint32_t baseHeight = static_cast<uint32_t>(bitmap.height());
312
313 // OpenGL's spec requires that each mipmap level have height/width equal to
314 // max(1, floor(original_height / 2^i)
315 // (or original_width) where i is the mipmap level.
316 // Continue scaling down until both axes are size 1.
317
318 const uint32_t largestAxis = SkTMax(baseWidth, baseHeight);
319 const int leadingZeros = SkCLZ(largestAxis);
320 // If the value 00011010 has 3 leading 0s then it has 5 significant bits
321 // (the bits which are not leading zeros)
322 const int significantBits = (sizeof(uint32_t) * 8) - leadingZeros;
323 // This is making the assumption that the size of a byte is 8 bits
324 // and that sizeof(uint32_t)'s implementation-defined behavior is 4.
325 if (significantBits < 0) {
326 return nullptr;
327 }
328 const uint32_t unsignedSignificantBits = static_cast<uint32_t>(significantBi ts);
329 const uint32_t mipLevelCount = unsignedSignificantBits;
bsalomon 2016/01/11 17:16:38 I believe everywhere this is passed takes an int n
Chris Blume 2016/01/12 12:49:33 Done.
330
331 const bool isMipMapped = mipLevelCount > 1;
332 desc.fIsMipMapped = isMipMapped;
333
334 SkTArray<SkBitmap> mipLevelBitmaps(mipLevelCount - 1);
335 mipLevelBitmaps.push_back_n(mipLevelCount - 1);
336
337 SkTArray<SkMipMapLevel> texels(mipLevelCount);
338
339 SkAutoLockPixels alp(bitmap);
340 if (!bitmap.readyToDraw()) {
341 return nullptr;
342 }
343
344 SkMipMapLevel baseLevel(bitmap.getPixels(), bitmap.rowBytes(), baseWidth, ba seHeight);
345 texels.push_back(baseLevel);
346
347 SkPaint paint;
348 paint.setFilterQuality(kMedium_SkFilterQuality);
349
350 for (uint32_t i = 1; i < mipLevelCount; i++) {
351 SkBitmap& currentMipBitmap = mipLevelBitmaps[i - 1];
352
353 uint32_t twoToTheMipLevel = 1 << i;
354 uint32_t currentMipLevelWidth = SkTMax(1u, baseWidth / twoToTheMipLevel) ;
355 uint32_t currentMipLevelHeight = SkTMax(1u, baseHeight / twoToTheMipLeve l);
356
357 SkImageInfo info = SkImageInfo::Make(currentMipLevelWidth, currentMipLev elHeight,
358 bitmap.colorType(), bitmap.alphaTyp e());
359 if (!currentMipBitmap.tryAllocPixels(info)) {
360 return nullptr;
361 }
362
363 SkCanvas canvas(currentMipBitmap);
364 canvas.clear(SK_ColorTRANSPARENT);
365
366 SkMatrix matrix;
367 matrix.setScale(static_cast<float>(currentMipLevelWidth) / baseWidth,
368 static_cast<float>(currentMipLevelHeight) / baseHeight);
369 canvas.save();
370 canvas.concat(matrix);
371 canvas.drawBitmap(bitmap, SkIntToScalar(0), SkIntToScalar(0), &paint);
372 canvas.restore();
373
374 SkMipMapLevel currentMipLevel(currentMipBitmap.getPixels(),
375 currentMipBitmap.rowBytes(),
376 currentMipLevelWidth, currentMipLevelHeigh t);
377 texels.push_back(currentMipLevel);
378 }
379
380 return ctx->textureProvider()->createTexture(desc, true, texels);
381 }
382
288 GrTexture* GrRefCachedBitmapTexture(GrContext* ctx, const SkBitmap& bitmap, 383 GrTexture* GrRefCachedBitmapTexture(GrContext* ctx, const SkBitmap& bitmap,
289 const GrTextureParams& params) { 384 const GrTextureParams& params) {
290 if (bitmap.getTexture()) { 385 if (bitmap.getTexture()) {
291 return GrBitmapTextureAdjuster(&bitmap).refTextureSafeForParams(params, nullptr); 386 return GrBitmapTextureAdjuster(&bitmap).refTextureSafeForParams(params, nullptr);
292 } 387 }
293 return GrBitmapTextureMaker(ctx, bitmap).refTextureForParams(params); 388 return GrBitmapTextureMaker(ctx, bitmap).refTextureForParams(params);
294 } 389 }
295 390
296 /////////////////////////////////////////////////////////////////////////////// 391 ///////////////////////////////////////////////////////////////////////////////
297 392
(...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after
627 SkErrorInternals::SetError( kInvalidPaint_SkError, 722 SkErrorInternals::SetError( kInvalidPaint_SkError,
628 "Sorry, I don't understand the filtering " 723 "Sorry, I don't understand the filtering "
629 "mode you asked for. Falling back to " 724 "mode you asked for. Falling back to "
630 "MIPMaps."); 725 "MIPMaps.");
631 textureFilterMode = GrTextureParams::kMipMap_FilterMode; 726 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
632 break; 727 break;
633 728
634 } 729 }
635 return textureFilterMode; 730 return textureFilterMode;
636 } 731 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698