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