2011-11-02 12:55:03 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 04:12:37 -07:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-11-02 12:55:03 -07:00
|
|
|
|
|
|
|
|
|
|
|
#include "Logging.h"
|
|
|
|
#include "SourceSurfaceSkia.h"
|
|
|
|
#include "skia/SkBitmap.h"
|
|
|
|
#include "skia/SkDevice.h"
|
|
|
|
#include "HelpersSkia.h"
|
2011-11-02 12:55:03 -07:00
|
|
|
#include "DrawTargetSkia.h"
|
2013-11-03 08:28:30 -08:00
|
|
|
#include "DataSurfaceHelpers.h"
|
2011-11-02 12:55:03 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
|
|
|
|
|
|
|
SourceSurfaceSkia::SourceSurfaceSkia()
|
2013-06-18 09:49:05 -07:00
|
|
|
: mDrawTarget(nullptr), mLocked(false)
|
2011-11-02 12:55:03 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceSurfaceSkia::~SourceSurfaceSkia()
|
|
|
|
{
|
2013-06-18 09:49:05 -07:00
|
|
|
MaybeUnlock();
|
2013-08-01 18:12:16 -07:00
|
|
|
if (mDrawTarget) {
|
|
|
|
mDrawTarget->SnapshotDestroyed();
|
|
|
|
mDrawTarget = nullptr;
|
|
|
|
}
|
2011-11-02 12:55:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
IntSize
|
|
|
|
SourceSurfaceSkia::GetSize() const
|
|
|
|
{
|
|
|
|
return mSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
SurfaceFormat
|
|
|
|
SourceSurfaceSkia::GetFormat() const
|
|
|
|
{
|
|
|
|
return mFormat;
|
|
|
|
}
|
|
|
|
|
2013-05-29 09:49:40 -07:00
|
|
|
bool
|
|
|
|
SourceSurfaceSkia::InitFromCanvas(SkCanvas* aCanvas,
|
|
|
|
SurfaceFormat aFormat,
|
|
|
|
DrawTargetSkia* aOwner)
|
|
|
|
{
|
|
|
|
SkISize size = aCanvas->getDeviceSize();
|
|
|
|
|
|
|
|
mBitmap = (SkBitmap)aCanvas->getDevice()->accessBitmap(false);
|
|
|
|
mFormat = aFormat;
|
|
|
|
|
|
|
|
mSize = IntSize(size.fWidth, size.fHeight);
|
|
|
|
mStride = mBitmap.rowBytes();
|
|
|
|
mDrawTarget = aOwner;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-11-02 12:55:03 -07:00
|
|
|
bool
|
|
|
|
SourceSurfaceSkia::InitFromData(unsigned char* aData,
|
|
|
|
const IntSize &aSize,
|
|
|
|
int32_t aStride,
|
|
|
|
SurfaceFormat aFormat)
|
|
|
|
{
|
2012-01-18 20:48:35 -08:00
|
|
|
SkBitmap temp;
|
2014-03-26 12:58:07 -07:00
|
|
|
temp.setConfig(GfxFormatToSkiaConfig(aFormat), aSize.width, aSize.height, aStride);
|
2012-01-18 20:48:35 -08:00
|
|
|
temp.setPixels(aData);
|
|
|
|
|
2014-03-20 11:07:36 -07:00
|
|
|
if (!temp.copyTo(&mBitmap, GfxFormatToSkiaColorType(aFormat))) {
|
2011-11-02 12:55:03 -07:00
|
|
|
return false;
|
|
|
|
}
|
2012-09-12 16:07:53 -07:00
|
|
|
|
2014-03-26 12:58:07 -07:00
|
|
|
if (aFormat == SurfaceFormat::B8G8R8X8) {
|
2014-04-17 14:01:23 -07:00
|
|
|
mBitmap.setAlphaType(kIgnore_SkAlphaType);
|
2014-03-26 12:58:07 -07:00
|
|
|
}
|
|
|
|
|
2011-11-02 12:55:03 -07:00
|
|
|
mSize = aSize;
|
|
|
|
mFormat = aFormat;
|
2013-12-02 08:03:13 -08:00
|
|
|
mStride = mBitmap.rowBytes();
|
2011-11-02 12:55:03 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char*
|
|
|
|
SourceSurfaceSkia::GetData()
|
|
|
|
{
|
2013-06-18 09:49:05 -07:00
|
|
|
if (!mLocked) {
|
|
|
|
mBitmap.lockPixels();
|
|
|
|
mLocked = true;
|
|
|
|
}
|
|
|
|
|
2011-11-02 12:55:03 -07:00
|
|
|
unsigned char *pixels = (unsigned char *)mBitmap.getPixels();
|
|
|
|
return pixels;
|
|
|
|
}
|
|
|
|
|
2011-11-02 12:55:03 -07:00
|
|
|
void
|
|
|
|
SourceSurfaceSkia::DrawTargetWillChange()
|
|
|
|
{
|
|
|
|
if (mDrawTarget) {
|
2013-06-18 09:49:05 -07:00
|
|
|
MaybeUnlock();
|
|
|
|
|
2012-08-14 11:06:12 -07:00
|
|
|
mDrawTarget = nullptr;
|
2011-11-02 12:55:03 -07:00
|
|
|
SkBitmap temp = mBitmap;
|
|
|
|
mBitmap.reset();
|
2014-03-20 11:07:36 -07:00
|
|
|
temp.copyTo(&mBitmap, temp.colorType());
|
2011-11-02 12:55:03 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-18 09:49:05 -07:00
|
|
|
void
|
|
|
|
SourceSurfaceSkia::MaybeUnlock()
|
|
|
|
{
|
|
|
|
if (mLocked) {
|
|
|
|
mBitmap.unlockPixels();
|
|
|
|
mLocked = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-02 12:55:03 -07:00
|
|
|
}
|
|
|
|
}
|