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"
|
2015-07-29 13:31:40 -07:00
|
|
|
#include "skia/include/core/SkBitmap.h"
|
|
|
|
#include "skia/include/core/SkDevice.h"
|
2011-11-02 12:55:03 -07:00
|
|
|
#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"
|
2015-07-29 13:31:40 -07:00
|
|
|
|
|
|
|
#ifdef USE_SKIA_GPU
|
|
|
|
#include "skia/include/gpu/SkGrPixelRef.h"
|
|
|
|
#endif
|
2011-11-02 12:55:03 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
|
|
|
|
|
|
|
SourceSurfaceSkia::SourceSurfaceSkia()
|
2015-05-26 00:00:19 -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();
|
2015-05-26 00:00:19 -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,
|
2015-05-26 00:00:19 -07:00
|
|
|
SurfaceFormat aFormat,
|
|
|
|
DrawTargetSkia* aOwner)
|
2013-05-29 09:49:40 -07:00
|
|
|
{
|
|
|
|
SkISize size = aCanvas->getDeviceSize();
|
|
|
|
|
|
|
|
mBitmap = (SkBitmap)aCanvas->getDevice()->accessBitmap(false);
|
2014-11-11 12:14:00 -08:00
|
|
|
|
2013-05-29 09:49:40 -07:00
|
|
|
mFormat = aFormat;
|
|
|
|
|
|
|
|
mSize = IntSize(size.fWidth, size.fHeight);
|
|
|
|
mStride = mBitmap.rowBytes();
|
2015-05-26 00:00:19 -07:00
|
|
|
mDrawTarget = aOwner;
|
2013-05-29 09:49:40 -07:00
|
|
|
|
|
|
|
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-07-27 17:47:43 -07: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-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;
|
|
|
|
}
|
|
|
|
|
2014-11-11 12:14:00 -08:00
|
|
|
bool
|
|
|
|
SourceSurfaceSkia::InitFromTexture(DrawTargetSkia* aOwner,
|
|
|
|
unsigned int aTexture,
|
|
|
|
const IntSize &aSize,
|
|
|
|
SurfaceFormat aFormat)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aOwner, "null GrContext");
|
2015-02-25 00:20:00 -08:00
|
|
|
#ifdef USE_SKIA_GPU
|
2014-11-11 12:14:00 -08:00
|
|
|
GrBackendTextureDesc skiaTexGlue;
|
|
|
|
mSize.width = skiaTexGlue.fWidth = aSize.width;
|
|
|
|
mSize.height = skiaTexGlue.fHeight = aSize.height;
|
|
|
|
skiaTexGlue.fFlags = kNone_GrBackendTextureFlag;
|
2015-09-23 17:27:58 -07:00
|
|
|
skiaTexGlue.fOrigin = kTopLeft_GrSurfaceOrigin;
|
2014-11-11 12:14:00 -08:00
|
|
|
skiaTexGlue.fConfig = GfxFormatToGrConfig(aFormat);
|
|
|
|
skiaTexGlue.fSampleCnt = 0;
|
|
|
|
skiaTexGlue.fTextureHandle = aTexture;
|
|
|
|
|
|
|
|
GrTexture *skiaTexture = aOwner->mGrContext->wrapBackendTexture(skiaTexGlue);
|
|
|
|
SkImageInfo imgInfo = SkImageInfo::Make(aSize.width, aSize.height, GfxFormatToSkiaColorType(aFormat), kOpaque_SkAlphaType);
|
|
|
|
SkGrPixelRef *texRef = new SkGrPixelRef(imgInfo, skiaTexture, false);
|
|
|
|
mBitmap.setInfo(imgInfo, aSize.width*aSize.height*4);
|
|
|
|
mBitmap.setPixelRef(texRef);
|
2015-02-25 00:20:00 -08:00
|
|
|
#endif
|
2014-11-11 12:14:00 -08:00
|
|
|
|
2015-05-26 00:00:19 -07:00
|
|
|
mDrawTarget = aOwner;
|
2014-11-11 12:14:00 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-11-02 12:55:03 -07:00
|
|
|
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()
|
|
|
|
{
|
2015-05-26 00:00:19 -07:00
|
|
|
if (mDrawTarget) {
|
2013-06-18 09:49:05 -07:00
|
|
|
MaybeUnlock();
|
|
|
|
|
2015-05-26 00:00:19 -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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-13 08:25:42 -07:00
|
|
|
} // namespace gfx
|
|
|
|
} // namespace mozilla
|