2011-11-02 23:55:03 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 15:12:37 +04: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 23:55:03 +04:00
|
|
|
|
|
|
|
|
|
|
|
#include "Logging.h"
|
|
|
|
#include "SourceSurfaceSkia.h"
|
|
|
|
#include "skia/SkBitmap.h"
|
|
|
|
#include "skia/SkDevice.h"
|
|
|
|
#include "HelpersSkia.h"
|
2011-11-02 23:55:03 +04:00
|
|
|
#include "DrawTargetSkia.h"
|
2013-11-03 20:28:30 +04:00
|
|
|
#include "DataSurfaceHelpers.h"
|
2011-11-02 23:55:03 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
|
|
|
|
|
|
|
SourceSurfaceSkia::SourceSurfaceSkia()
|
2013-06-18 20:49:05 +04:00
|
|
|
: mDrawTarget(nullptr), mLocked(false)
|
2011-11-02 23:55:03 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceSurfaceSkia::~SourceSurfaceSkia()
|
|
|
|
{
|
2013-06-18 20:49:05 +04:00
|
|
|
MaybeUnlock();
|
2013-08-02 05:12:16 +04:00
|
|
|
if (mDrawTarget) {
|
|
|
|
mDrawTarget->SnapshotDestroyed();
|
|
|
|
mDrawTarget = nullptr;
|
|
|
|
}
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
IntSize
|
|
|
|
SourceSurfaceSkia::GetSize() const
|
|
|
|
{
|
|
|
|
return mSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
SurfaceFormat
|
|
|
|
SourceSurfaceSkia::GetFormat() const
|
|
|
|
{
|
|
|
|
return mFormat;
|
|
|
|
}
|
|
|
|
|
2013-05-29 20:49:40 +04: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 23:55:03 +04:00
|
|
|
bool
|
|
|
|
SourceSurfaceSkia::InitFromData(unsigned char* aData,
|
|
|
|
const IntSize &aSize,
|
|
|
|
int32_t aStride,
|
|
|
|
SurfaceFormat aFormat)
|
|
|
|
{
|
2012-01-19 08:48:35 +04:00
|
|
|
SkBitmap temp;
|
2014-07-28 04:47:43 +04:00
|
|
|
SkAlphaType alphaType = (aFormat == SurfaceFormat::B8G8R8X8) ?
|
|
|
|
kOpaque_SkAlphaType : kPremul_SkAlphaType;
|
|
|
|
|
|
|
|
SkImageInfo info = SkImageInfo::Make(aSize.width,
|
|
|
|
aSize.height,
|
|
|
|
GfxFormatToSkiaColorType(aFormat),
|
|
|
|
alphaType);
|
|
|
|
temp.setInfo(info, aStride);
|
2012-01-19 08:48:35 +04:00
|
|
|
temp.setPixels(aData);
|
|
|
|
|
2014-03-20 22:07:36 +04:00
|
|
|
if (!temp.copyTo(&mBitmap, GfxFormatToSkiaColorType(aFormat))) {
|
2011-11-02 23:55:03 +04:00
|
|
|
return false;
|
|
|
|
}
|
2012-09-13 03:07:53 +04:00
|
|
|
|
2014-03-26 23:58:07 +04:00
|
|
|
if (aFormat == SurfaceFormat::B8G8R8X8) {
|
2014-04-18 01:01:23 +04:00
|
|
|
mBitmap.setAlphaType(kIgnore_SkAlphaType);
|
2014-03-26 23:58:07 +04:00
|
|
|
}
|
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
mSize = aSize;
|
|
|
|
mFormat = aFormat;
|
2013-12-02 20:03:13 +04:00
|
|
|
mStride = mBitmap.rowBytes();
|
2011-11-02 23:55:03 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char*
|
|
|
|
SourceSurfaceSkia::GetData()
|
|
|
|
{
|
2013-06-18 20:49:05 +04:00
|
|
|
if (!mLocked) {
|
|
|
|
mBitmap.lockPixels();
|
|
|
|
mLocked = true;
|
|
|
|
}
|
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
unsigned char *pixels = (unsigned char *)mBitmap.getPixels();
|
|
|
|
return pixels;
|
|
|
|
}
|
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
void
|
|
|
|
SourceSurfaceSkia::DrawTargetWillChange()
|
|
|
|
{
|
|
|
|
if (mDrawTarget) {
|
2013-06-18 20:49:05 +04:00
|
|
|
MaybeUnlock();
|
|
|
|
|
2012-08-14 22:06:12 +04:00
|
|
|
mDrawTarget = nullptr;
|
2011-11-02 23:55:03 +04:00
|
|
|
SkBitmap temp = mBitmap;
|
|
|
|
mBitmap.reset();
|
2014-03-20 22:07:36 +04:00
|
|
|
temp.copyTo(&mBitmap, temp.colorType());
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-18 20:49:05 +04:00
|
|
|
void
|
|
|
|
SourceSurfaceSkia::MaybeUnlock()
|
|
|
|
{
|
|
|
|
if (mLocked) {
|
|
|
|
mBitmap.unlockPixels();
|
|
|
|
mLocked = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-02 23:55:03 +04:00
|
|
|
}
|
|
|
|
}
|