michael@0: /* michael@0: * Copyright 2010 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "SkGr.h" michael@0: #include "SkConfig8888.h" michael@0: #include "SkMessageBus.h" michael@0: #include "SkPixelRef.h" michael@0: #include "GrResourceCache.h" michael@0: michael@0: /* Fill out buffer with the compressed format Ganesh expects from a colortable michael@0: based bitmap. [palette (colortable) + indices]. michael@0: michael@0: At the moment Ganesh only supports 8bit version. If Ganesh allowed we others michael@0: we could detect that the colortable.count is <= 16, and then repack the michael@0: indices as nibbles to save RAM, but it would take more time (i.e. a lot michael@0: slower than memcpy), so skipping that for now. michael@0: michael@0: Ganesh wants a full 256 palette entry, even though Skia's ctable is only as big michael@0: as the colortable.count says it is. michael@0: */ michael@0: static void build_compressed_data(void* buffer, const SkBitmap& bitmap) { michael@0: SkASSERT(SkBitmap::kIndex8_Config == bitmap.config()); michael@0: michael@0: SkAutoLockPixels alp(bitmap); michael@0: if (!bitmap.readyToDraw()) { michael@0: SkDEBUGFAIL("bitmap not ready to draw!"); michael@0: return; michael@0: } michael@0: michael@0: SkColorTable* ctable = bitmap.getColorTable(); michael@0: char* dst = (char*)buffer; michael@0: michael@0: uint32_t* colorTableDst = reinterpret_cast(dst); michael@0: const uint32_t* colorTableSrc = reinterpret_cast(ctable->lockColors()); michael@0: SkConvertConfig8888Pixels(colorTableDst, 0, SkCanvas::kRGBA_Premul_Config8888, michael@0: colorTableSrc, 0, SkCanvas::kNative_Premul_Config8888, michael@0: ctable->count(), 1); michael@0: ctable->unlockColors(); michael@0: michael@0: // always skip a full 256 number of entries, even if we memcpy'd fewer michael@0: dst += kGrColorTableSize; michael@0: michael@0: if ((unsigned)bitmap.width() == bitmap.rowBytes()) { michael@0: memcpy(dst, bitmap.getPixels(), bitmap.getSize()); michael@0: } else { michael@0: // need to trim off the extra bytes per row michael@0: size_t width = bitmap.width(); michael@0: size_t rowBytes = bitmap.rowBytes(); michael@0: const char* src = (const char*)bitmap.getPixels(); michael@0: for (int y = 0; y < bitmap.height(); y++) { michael@0: memcpy(dst, src, width); michael@0: src += rowBytes; michael@0: dst += width; michael@0: } michael@0: } michael@0: } michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: static void generate_bitmap_cache_id(const SkBitmap& bitmap, GrCacheID* id) { michael@0: // Our id includes the offset, width, and height so that bitmaps created by extractSubset() michael@0: // are unique. michael@0: uint32_t genID = bitmap.getGenerationID(); michael@0: SkIPoint origin = bitmap.pixelRefOrigin(); michael@0: int16_t width = SkToS16(bitmap.width()); michael@0: int16_t height = SkToS16(bitmap.height()); michael@0: michael@0: GrCacheID::Key key; michael@0: memcpy(key.fData8 + 0, &genID, 4); michael@0: memcpy(key.fData8 + 4, &origin.fX, 4); michael@0: memcpy(key.fData8 + 8, &origin.fY, 4); michael@0: memcpy(key.fData8 + 12, &width, 2); michael@0: memcpy(key.fData8 + 14, &height, 2); michael@0: static const size_t kKeyDataSize = 16; michael@0: memset(key.fData8 + kKeyDataSize, 0, sizeof(key) - kKeyDataSize); michael@0: GR_STATIC_ASSERT(sizeof(key) >= kKeyDataSize); michael@0: static const GrCacheID::Domain gBitmapTextureDomain = GrCacheID::GenerateDomain(); michael@0: id->reset(gBitmapTextureDomain, key); michael@0: } michael@0: michael@0: static void generate_bitmap_texture_desc(const SkBitmap& bitmap, GrTextureDesc* desc) { michael@0: desc->fFlags = kNone_GrTextureFlags; michael@0: desc->fWidth = bitmap.width(); michael@0: desc->fHeight = bitmap.height(); michael@0: desc->fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config()); michael@0: desc->fSampleCnt = 0; michael@0: } michael@0: michael@0: namespace { michael@0: michael@0: // When the SkPixelRef genID changes, invalidate a corresponding GrResource described by key. michael@0: class GrResourceInvalidator : public SkPixelRef::GenIDChangeListener { michael@0: public: michael@0: explicit GrResourceInvalidator(GrResourceKey key) : fKey(key) {} michael@0: private: michael@0: GrResourceKey fKey; michael@0: michael@0: virtual void onChange() SK_OVERRIDE { michael@0: const GrResourceInvalidatedMessage message = { fKey }; michael@0: SkMessageBus::Post(message); michael@0: } michael@0: }; michael@0: michael@0: } // namespace michael@0: michael@0: static void add_genID_listener(GrResourceKey key, SkPixelRef* pixelRef) { michael@0: SkASSERT(NULL != pixelRef); michael@0: pixelRef->addGenIDChangeListener(SkNEW_ARGS(GrResourceInvalidator, (key))); michael@0: } michael@0: michael@0: static GrTexture* sk_gr_create_bitmap_texture(GrContext* ctx, michael@0: bool cache, michael@0: const GrTextureParams* params, michael@0: const SkBitmap& origBitmap) { michael@0: SkBitmap tmpBitmap; michael@0: michael@0: const SkBitmap* bitmap = &origBitmap; michael@0: michael@0: GrTextureDesc desc; michael@0: generate_bitmap_texture_desc(*bitmap, &desc); michael@0: michael@0: if (SkBitmap::kIndex8_Config == bitmap->config()) { michael@0: // build_compressed_data doesn't do npot->pot expansion michael@0: // and paletted textures can't be sub-updated michael@0: if (ctx->supportsIndex8PixelConfig(params, bitmap->width(), bitmap->height())) { michael@0: size_t imagesize = bitmap->width() * bitmap->height() + kGrColorTableSize; michael@0: SkAutoMalloc storage(imagesize); michael@0: michael@0: build_compressed_data(storage.get(), origBitmap); michael@0: michael@0: // our compressed data will be trimmed, so pass width() for its michael@0: // "rowBytes", since they are the same now. michael@0: michael@0: if (cache) { michael@0: GrCacheID cacheID; michael@0: generate_bitmap_cache_id(origBitmap, &cacheID); michael@0: michael@0: GrResourceKey key; michael@0: GrTexture* result = ctx->createTexture(params, desc, cacheID, michael@0: storage.get(), bitmap->width(), &key); michael@0: if (NULL != result) { michael@0: add_genID_listener(key, origBitmap.pixelRef()); michael@0: } michael@0: return result; michael@0: } else { michael@0: GrTexture* result = ctx->lockAndRefScratchTexture(desc, michael@0: GrContext::kExact_ScratchTexMatch); michael@0: result->writePixels(0, 0, bitmap->width(), michael@0: bitmap->height(), desc.fConfig, michael@0: storage.get()); michael@0: return result; michael@0: } michael@0: } else { michael@0: origBitmap.copyTo(&tmpBitmap, kPMColor_SkColorType); michael@0: // now bitmap points to our temp, which has been promoted to 32bits michael@0: bitmap = &tmpBitmap; michael@0: desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap->config()); michael@0: } michael@0: } michael@0: michael@0: SkAutoLockPixels alp(*bitmap); michael@0: if (!bitmap->readyToDraw()) { michael@0: return NULL; michael@0: } michael@0: if (cache) { michael@0: // This texture is likely to be used again so leave it in the cache michael@0: GrCacheID cacheID; michael@0: generate_bitmap_cache_id(origBitmap, &cacheID); michael@0: michael@0: GrResourceKey key; michael@0: GrTexture* result = ctx->createTexture(params, desc, cacheID, michael@0: bitmap->getPixels(), bitmap->rowBytes(), &key); michael@0: if (NULL != result) { michael@0: add_genID_listener(key, origBitmap.pixelRef()); michael@0: } michael@0: return result; michael@0: } else { michael@0: // This texture is unlikely to be used again (in its present form) so michael@0: // just use a scratch texture. This will remove the texture from the michael@0: // cache so no one else can find it. Additionally, once unlocked, the michael@0: // scratch texture will go to the end of the list for purging so will michael@0: // likely be available for this volatile bitmap the next time around. michael@0: GrTexture* result = ctx->lockAndRefScratchTexture(desc, GrContext::kExact_ScratchTexMatch); michael@0: result->writePixels(0, 0, michael@0: bitmap->width(), bitmap->height(), michael@0: desc.fConfig, michael@0: bitmap->getPixels(), michael@0: bitmap->rowBytes()); michael@0: return result; michael@0: } michael@0: } michael@0: michael@0: bool GrIsBitmapInCache(const GrContext* ctx, michael@0: const SkBitmap& bitmap, michael@0: const GrTextureParams* params) { michael@0: GrCacheID cacheID; michael@0: generate_bitmap_cache_id(bitmap, &cacheID); michael@0: michael@0: GrTextureDesc desc; michael@0: generate_bitmap_texture_desc(bitmap, &desc); michael@0: return ctx->isTextureInCache(desc, cacheID, params); michael@0: } michael@0: michael@0: GrTexture* GrLockAndRefCachedBitmapTexture(GrContext* ctx, michael@0: const SkBitmap& bitmap, michael@0: const GrTextureParams* params) { michael@0: GrTexture* result = NULL; michael@0: michael@0: bool cache = !bitmap.isVolatile(); michael@0: michael@0: if (cache) { michael@0: // If the bitmap isn't changing try to find a cached copy first. michael@0: michael@0: GrCacheID cacheID; michael@0: generate_bitmap_cache_id(bitmap, &cacheID); michael@0: michael@0: GrTextureDesc desc; michael@0: generate_bitmap_texture_desc(bitmap, &desc); michael@0: michael@0: result = ctx->findAndRefTexture(desc, cacheID, params); michael@0: } michael@0: if (NULL == result) { michael@0: result = sk_gr_create_bitmap_texture(ctx, cache, params, bitmap); michael@0: } michael@0: if (NULL == result) { michael@0: GrPrintf("---- failed to create texture for cache [%d %d]\n", michael@0: bitmap.width(), bitmap.height()); michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: void GrUnlockAndUnrefCachedBitmapTexture(GrTexture* texture) { michael@0: SkASSERT(NULL != texture->getContext()); michael@0: michael@0: texture->getContext()->unlockScratchTexture(texture); michael@0: texture->unref(); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: GrPixelConfig SkBitmapConfig2GrPixelConfig(SkBitmap::Config config) { michael@0: switch (config) { michael@0: case SkBitmap::kA8_Config: michael@0: return kAlpha_8_GrPixelConfig; michael@0: case SkBitmap::kIndex8_Config: michael@0: return kIndex_8_GrPixelConfig; michael@0: case SkBitmap::kRGB_565_Config: michael@0: return kRGB_565_GrPixelConfig; michael@0: case SkBitmap::kARGB_4444_Config: michael@0: return kRGBA_4444_GrPixelConfig; michael@0: case SkBitmap::kARGB_8888_Config: michael@0: return kSkia8888_GrPixelConfig; michael@0: default: michael@0: // kNo_Config, kA1_Config missing michael@0: return kUnknown_GrPixelConfig; michael@0: } michael@0: } michael@0: michael@0: // alphatype is ignore for now, but if GrPixelConfig is expanded to encompass michael@0: // alpha info, that will be considered. michael@0: GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType) { michael@0: switch (ct) { michael@0: case kUnknown_SkColorType: michael@0: return kUnknown_GrPixelConfig; michael@0: case kAlpha_8_SkColorType: michael@0: return kAlpha_8_GrPixelConfig; michael@0: case kRGB_565_SkColorType: michael@0: return kRGB_565_GrPixelConfig; michael@0: case kARGB_4444_SkColorType: michael@0: return kRGBA_4444_GrPixelConfig; michael@0: case kRGBA_8888_SkColorType: michael@0: return kRGBA_8888_GrPixelConfig; michael@0: case kBGRA_8888_SkColorType: michael@0: return kBGRA_8888_GrPixelConfig; michael@0: case kIndex_8_SkColorType: michael@0: return kIndex_8_GrPixelConfig; michael@0: } michael@0: SkASSERT(0); // shouldn't get here michael@0: return kUnknown_GrPixelConfig; michael@0: } michael@0: michael@0: bool GrPixelConfig2ColorType(GrPixelConfig config, SkColorType* ctOut) { michael@0: SkColorType ct; michael@0: switch (config) { michael@0: case kAlpha_8_GrPixelConfig: michael@0: ct = kAlpha_8_SkColorType; michael@0: break; michael@0: case kIndex_8_GrPixelConfig: michael@0: ct = kIndex_8_SkColorType; michael@0: break; michael@0: case kRGB_565_GrPixelConfig: michael@0: ct = kRGB_565_SkColorType; michael@0: break; michael@0: case kRGBA_4444_GrPixelConfig: michael@0: ct = kARGB_4444_SkColorType; michael@0: break; michael@0: case kRGBA_8888_GrPixelConfig: michael@0: ct = kRGBA_8888_SkColorType; michael@0: break; michael@0: case kBGRA_8888_GrPixelConfig: michael@0: ct = kBGRA_8888_SkColorType; michael@0: break; michael@0: default: michael@0: return false; michael@0: } michael@0: if (ctOut) { michael@0: *ctOut = ct; michael@0: } michael@0: return true; michael@0: }