void TextureCache::generateTexture(SkBitmap* bitmap, Texture* texture, bool regenerate) { SkAutoLockPixels alp(*bitmap); if (!bitmap->readyToDraw()) { ALOGE("Cannot generate texture from bitmap"); return; } const bool resize = !regenerate || bitmap->width() != int(texture->width) || bitmap->height() != int(texture->height); if (!regenerate) { glGenTextures(1, &texture->id); } texture->generation = bitmap->getGenerationID(); texture->width = bitmap->width(); texture->height = bitmap->height(); glBindTexture(GL_TEXTURE_2D, texture->id); if (!regenerate) { glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel()); } switch (bitmap->getConfig()) { case SkBitmap::kA8_Config: if (!regenerate) { glPixelStorei(GL_UNPACK_ALIGNMENT, 1); } uploadToTexture(resize, GL_ALPHA, bitmap->rowBytesAsPixels(), texture->height, GL_UNSIGNED_BYTE, bitmap->getPixels()); texture->blend = true; break; case SkBitmap::kRGB_565_Config: uploadToTexture(resize, GL_RGB, bitmap->rowBytesAsPixels(), texture->height, GL_UNSIGNED_SHORT_5_6_5, bitmap->getPixels()); texture->blend = false; break; case SkBitmap::kARGB_8888_Config: uploadToTexture(resize, GL_RGBA, bitmap->rowBytesAsPixels(), texture->height, GL_UNSIGNED_BYTE, bitmap->getPixels()); // Do this after calling getPixels() to make sure Skia's deferred // decoding happened texture->blend = !bitmap->isOpaque(); break; case SkBitmap::kARGB_4444_Config: case SkBitmap::kIndex8_Config: uploadLoFiTexture(resize, bitmap, texture->width, texture->height); texture->blend = !bitmap->isOpaque(); break; default: ALOGW("Unsupported bitmap config: %d", bitmap->getConfig()); break; } if (!regenerate) { texture->setFilter(GL_NEAREST); texture->setWrap(GL_CLAMP_TO_EDGE); } }
void TextureCache::uploadLoFiTexture(bool resize, const SkBitmap* bitmap, uint32_t width, uint32_t height) { SkBitmap rgbaBitmap; rgbaBitmap.allocPixels(SkImageInfo::MakeN32(width, height, bitmap->alphaType())); rgbaBitmap.eraseColor(0); SkCanvas canvas(rgbaBitmap); canvas.drawBitmap(*bitmap, 0.0f, 0.0f, nullptr); uploadToTexture(resize, GL_RGBA, rgbaBitmap.rowBytesAsPixels(), rgbaBitmap.bytesPerPixel(), width, height, GL_UNSIGNED_BYTE, rgbaBitmap.getPixels()); }
void TextureCache::uploadLoFiTexture(bool resize, SkBitmap* bitmap, uint32_t width, uint32_t height) { SkBitmap rgbaBitmap; rgbaBitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height, 0, bitmap->alphaType()); rgbaBitmap.allocPixels(); rgbaBitmap.eraseColor(0); SkCanvas canvas(rgbaBitmap); canvas.drawBitmap(*bitmap, 0.0f, 0.0f, NULL); uploadToTexture(resize, GL_RGBA, rgbaBitmap.rowBytesAsPixels(), width, height, GL_UNSIGNED_BYTE, rgbaBitmap.getPixels()); }
void TextureCache::generateTexture(const SkBitmap* bitmap, Texture* texture, bool regenerate) { SkAutoLockPixels alp(*bitmap); if (!bitmap->readyToDraw()) { ALOGE("Cannot generate texture from bitmap"); return; } ATRACE_FORMAT("Upload %ux%u Texture", bitmap->width(), bitmap->height()); // We could also enable mipmapping if both bitmap dimensions are powers // of 2 but we'd have to deal with size changes. Let's keep this simple const bool canMipMap = Caches::getInstance().extensions().hasNPot(); // If the texture had mipmap enabled but not anymore, // force a glTexImage2D to discard the mipmap levels const bool resize = !regenerate || bitmap->width() != int(texture->width) || bitmap->height() != int(texture->height) || (regenerate && canMipMap && texture->mipMap && !bitmap->hasHardwareMipMap()); if (!regenerate) { glGenTextures(1, &texture->id); } texture->generation = bitmap->getGenerationID(); texture->width = bitmap->width(); texture->height = bitmap->height(); Caches::getInstance().textureState().bindTexture(texture->id); switch (bitmap->colorType()) { case kAlpha_8_SkColorType: uploadToTexture(resize, GL_ALPHA, bitmap->rowBytesAsPixels(), bitmap->bytesPerPixel(), texture->width, texture->height, GL_UNSIGNED_BYTE, bitmap->getPixels()); texture->blend = true; break; case kRGB_565_SkColorType: uploadToTexture(resize, GL_RGB, bitmap->rowBytesAsPixels(), bitmap->bytesPerPixel(), texture->width, texture->height, GL_UNSIGNED_SHORT_5_6_5, bitmap->getPixels()); texture->blend = false; break; case kN32_SkColorType: uploadToTexture(resize, GL_RGBA, bitmap->rowBytesAsPixels(), bitmap->bytesPerPixel(), texture->width, texture->height, GL_UNSIGNED_BYTE, bitmap->getPixels()); // Do this after calling getPixels() to make sure Skia's deferred // decoding happened texture->blend = !bitmap->isOpaque(); break; case kARGB_4444_SkColorType: case kIndex_8_SkColorType: uploadLoFiTexture(resize, bitmap, texture->width, texture->height); texture->blend = !bitmap->isOpaque(); break; default: ALOGW("Unsupported bitmap colorType: %d", bitmap->colorType()); break; } if (canMipMap) { texture->mipMap = bitmap->hasHardwareMipMap(); if (texture->mipMap) { glGenerateMipmap(GL_TEXTURE_2D); } } if (!regenerate) { texture->setFilter(GL_NEAREST); texture->setWrap(GL_CLAMP_TO_EDGE); } }