bool SourceSurfaceSkia::InitFromData(unsigned char* aData, const IntSize &aSize, int32_t aStride, SurfaceFormat aFormat) { SkBitmap temp; temp.setConfig(GfxFormatToSkiaConfig(aFormat), aSize.width, aSize.height, aStride); temp.setPixels(aData); if (!temp.copyTo(&mBitmap, GfxFormatToSkiaConfig(aFormat))) { return false; } if (aFormat == FORMAT_B8G8R8X8) { mBitmap.lockPixels(); // We have to manually set the A channel to be 255 as Skia doesn't understand BGRX ConvertBGRXToBGRA(reinterpret_cast<unsigned char*>(mBitmap.getPixels()), aSize, aStride); mBitmap.unlockPixels(); mBitmap.notifyPixelsChanged(); mBitmap.setIsOpaque(true); } mSize = aSize; mFormat = aFormat; mStride = aStride; return true; }
uint8_t* SurfaceToPackedBGRA(DataSourceSurface *aSurface) { SurfaceFormat format = aSurface->GetFormat(); if (format != SurfaceFormat::B8G8R8A8 && format != SurfaceFormat::B8G8R8X8) { return nullptr; } IntSize size = aSurface->GetSize(); uint8_t* imageBuffer = new (std::nothrow) uint8_t[size.width * size.height * sizeof(uint32_t)]; if (!imageBuffer) { return nullptr; } DataSourceSurface::MappedSurface map; if (!aSurface->Map(DataSourceSurface::MapType::READ, &map)) { delete [] imageBuffer; return nullptr; } CopySurfaceDataToPackedArray(map.mData, imageBuffer, size, map.mStride, 4 * sizeof(uint8_t)); aSurface->Unmap(); if (format == SurfaceFormat::B8G8R8X8) { // Convert BGRX to BGRA by setting a to 255. ConvertBGRXToBGRA(reinterpret_cast<uint8_t *>(imageBuffer), size, size.width * sizeof(uint32_t)); } return imageBuffer; }
void DrawTargetSkia::Init(unsigned char* aData, const IntSize &aSize, int32_t aStride, SurfaceFormat aFormat) { SkAlphaType alphaType = kPremul_SkAlphaType; if (aFormat == SurfaceFormat::B8G8R8X8) { // We have to manually set the A channel to be 255 as Skia doesn't understand BGRX ConvertBGRXToBGRA(aData, aSize, aStride); alphaType = kOpaque_SkAlphaType; } SkBitmap bitmap; bitmap.setConfig(GfxFormatToSkiaConfig(aFormat), aSize.width, aSize.height, aStride, alphaType); bitmap.setPixels(aData); SkAutoTUnref<SkCanvas> canvas(new SkCanvas(new SkBitmapDevice(bitmap))); mSize = aSize; mCanvas = canvas.get(); mFormat = aFormat; }
void DrawTargetSkia::Init(unsigned char* aData, const IntSize &aSize, int32_t aStride, SurfaceFormat aFormat) { if (aFormat == FORMAT_B8G8R8X8) { // We have to manually set the A channel to be 255 as Skia doesn't understand BGRX ConvertBGRXToBGRA(aData, aSize, aStride); mBitmap.setIsOpaque(true); } mBitmap.setConfig(GfxFormatToSkiaConfig(aFormat), aSize.width, aSize.height, aStride); mBitmap.setPixels(aData); SkAutoTUnref<SkDevice> device(new SkDevice(mBitmap)); SkAutoTUnref<SkCanvas> canvas(new SkCanvas(device.get())); mSize = aSize; mDevice = device.get(); mCanvas = canvas.get(); mFormat = aFormat; }