Bug 1044102 - Part 1 - Implement ImageBitmap. r=roc, sr=smaug

--HG--
extra : rebase_source : c26f327064125a6d5690b03571f25ea0e25347eb
This commit is contained in:
Kaku Kuo 2015-07-30 20:47:00 +02:00
parent b6da468928
commit 9c3f995e43
14 changed files with 1372 additions and 0 deletions

View File

@ -219,6 +219,7 @@
#include "mozilla/dom/MediaQueryList.h"
#include "mozilla/dom/ScriptSettings.h"
#include "mozilla/dom/NavigatorBinding.h"
#include "mozilla/dom/ImageBitmap.h"
#ifdef HAVE_SIDEBAR
#include "mozilla/dom/ExternalBinding.h"
#endif
@ -14665,3 +14666,18 @@ nsGlobalWindow::FireOnNewGlobalObject()
#ifdef _WINDOWS_
#error "Never include windows.h in this file!"
#endif
already_AddRefed<Promise>
nsGlobalWindow::CreateImageBitmap(const ImageBitmapSource& aImage,
ErrorResult& aRv)
{
return ImageBitmap::Create(this, aImage, Nothing(), aRv);
}
already_AddRefed<Promise>
nsGlobalWindow::CreateImageBitmap(const ImageBitmapSource& aImage,
int32_t aSx, int32_t aSy, int32_t aSw, int32_t aSh,
ErrorResult& aRv)
{
return ImageBitmap::Create(this, aImage, Some(gfx::IntRect(aSx, aSy, aSw, aSh)), aRv);
}

View File

@ -52,6 +52,7 @@
#include "nsComponentManagerUtils.h"
#include "nsSize.h"
#include "nsCheapSets.h"
#include "mozilla/dom/ImageBitmapSource.h"
#define DEFAULT_HOME_PAGE "www.mozilla.org"
#define PREF_BROWSER_STARTUP_HOMEPAGE "browser.startup.homepage"
@ -1132,6 +1133,15 @@ public:
GetContent(aCx, aRetval, aError);
}
already_AddRefed<mozilla::dom::Promise>
CreateImageBitmap(const mozilla::dom::ImageBitmapSource& aImage,
mozilla::ErrorResult& aRv);
already_AddRefed<mozilla::dom::Promise>
CreateImageBitmap(const mozilla::dom::ImageBitmapSource& aImage,
int32_t aSx, int32_t aSy, int32_t aSw, int32_t aSh,
mozilla::ErrorResult& aRv);
// ChromeWindow bits. Do NOT call these unless your window is in
// fact an nsGlobalChromeWindow.
uint16_t WindowState();

1090
dom/canvas/ImageBitmap.cpp Normal file

File diff suppressed because it is too large Load Diff

162
dom/canvas/ImageBitmap.h Normal file
View File

@ -0,0 +1,162 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* 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/. */
#ifndef mozilla_dom_ImageBitmap_h
#define mozilla_dom_ImageBitmap_h
#include "mozilla/Attributes.h"
#include "mozilla/dom/ImageBitmapSource.h"
#include "mozilla/dom/TypedArray.h"
#include "mozilla/gfx/Rect.h"
#include "mozilla/Maybe.h"
#include "nsCycleCollectionParticipant.h"
struct JSContext;
namespace mozilla {
class ErrorResult;
namespace gfx {
class SourceSurface;
}
namespace layers {
class Image;
}
namespace dom {
class CanvasRenderingContext2D;
class File;
class HTMLCanvasElement;
class HTMLImageElement;
class HTMLVideoElement;
class ImageData;
class Promise;
class CreateImageBitmapFromBlob;
class CreateImageBitmapFromBlobTask;
class CreateImageBitmapFromBlobWorkerTask;
/*
* ImageBitmap is an opaque handler to several kinds of image-like objects from
* HTMLImageElement, HTMLVideoElement, HTMLCanvasElement, ImageData to
* CanvasRenderingContext2D and Image Blob.
*
* An ImageBitmap could be painted to a canvas element.
*
* Generally, an ImageBitmap only keeps a reference to its source object's
* buffer, but if the source object is an ImageData, an Blob or a
* HTMLCanvasElement with WebGL rendering context, the ImageBitmap copy the
* source object's buffer.
*/
class ImageBitmap final : public nsISupports,
public nsWrapperCache
{
public:
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(ImageBitmap)
nsCOMPtr<nsIGlobalObject> GetParentObject() const { return mParent; }
virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
uint32_t Width() const
{
return mPictureRect.Width();
}
uint32_t Height() const
{
return mPictureRect.Height();
}
/*
* The PrepareForDrawTarget() might return null if the mPictureRect does not
* intersect with the size of mData.
*/
already_AddRefed<gfx::SourceSurface>
PrepareForDrawTarget(gfx::DrawTarget* aTarget);
static already_AddRefed<Promise>
Create(nsIGlobalObject* aGlobal, const ImageBitmapSource& aSrc,
const Maybe<gfx::IntRect>& aCropRect, ErrorResult& aRv);
friend CreateImageBitmapFromBlob;
friend CreateImageBitmapFromBlobTask;
friend CreateImageBitmapFromBlobWorkerTask;
protected:
ImageBitmap(nsIGlobalObject* aGlobal, layers::Image* aData);
virtual ~ImageBitmap();
void SetPictureRect(const gfx::IntRect& aRect, ErrorResult& aRv);
static already_AddRefed<ImageBitmap>
CreateInternal(nsIGlobalObject* aGlobal, HTMLImageElement& aImageEl,
const Maybe<gfx::IntRect>& aCropRect, ErrorResult& aRv);
static already_AddRefed<ImageBitmap>
CreateInternal(nsIGlobalObject* aGlobal, HTMLVideoElement& aVideoEl,
const Maybe<gfx::IntRect>& aCropRect, ErrorResult& aRv);
static already_AddRefed<ImageBitmap>
CreateInternal(nsIGlobalObject* aGlobal, HTMLCanvasElement& aCanvasEl,
const Maybe<gfx::IntRect>& aCropRect, ErrorResult& aRv);
static already_AddRefed<ImageBitmap>
CreateInternal(nsIGlobalObject* aGlobal, ImageData& aImageData,
const Maybe<gfx::IntRect>& aCropRect, ErrorResult& aRv);
static already_AddRefed<ImageBitmap>
CreateInternal(nsIGlobalObject* aGlobal, CanvasRenderingContext2D& aCanvasCtx,
const Maybe<gfx::IntRect>& aCropRect, ErrorResult& aRv);
static already_AddRefed<ImageBitmap>
CreateInternal(nsIGlobalObject* aGlobal, ImageBitmap& aImageBitmap,
const Maybe<gfx::IntRect>& aCropRect, ErrorResult& aRv);
nsCOMPtr<nsIGlobalObject> mParent;
/*
* The mData is the data buffer of an ImageBitmap, so the mData must not be
* null.
*
* The mSurface is a cache for drawing the ImageBitmap onto a
* HTMLCanvasElement. The mSurface is null while the ImageBitmap is created
* and then will be initialized while the PrepareForDrawTarget() method is
* called first time.
*
* The mSurface might just be a reference to the same data buffer of the mData
* if the are of mPictureRect is just the same as the mData's size. Or, it is
* a independent data buffer which is copied and cropped form the mData's data
* buffer.
*/
nsRefPtr<layers::Image> mData;
RefPtr<gfx::SourceSurface> mSurface;
/*
* The mPictureRect is the size of the source image in default, however, if
* users specify the cropping area while creating an ImageBitmap, then this
* mPictureRect is the cropping area.
*
* Note that if the CreateInternal() copies and crops data from the source
* image, then this mPictureRect is just the size of the final mData.
*
* The mPictureRect will be used at PrepareForDrawTarget() while user is going
* to draw this ImageBitmap into a HTMLCanvasElement.
*/
gfx::IntRect mPictureRect;
};
} // namespace dom
} // namespace mozilla
#endif // mozilla_dom_ImageBitmap_h

View File

@ -0,0 +1,21 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* 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/. */
#ifndef mozilla_dom_ImageBitmapSource_h
#define mozilla_dom_ImageBitmapSource_h
namespace mozilla {
namespace dom {
// So we don't have to forward declare this elsewhere.
class HTMLImageElementOrHTMLVideoElementOrHTMLCanvasElementOrBlobOrImageDataOrCanvasRenderingContext2DOrImageBitmap;
typedef HTMLImageElementOrHTMLVideoElementOrHTMLCanvasElementOrBlobOrImageDataOrCanvasRenderingContext2DOrImageBitmap
ImageBitmapSource;
}
}
#endif

View File

@ -29,6 +29,8 @@ EXPORTS.mozilla.dom += [
'CanvasPattern.h',
'CanvasRenderingContext2D.h',
'CanvasUtils.h',
'ImageBitmap.h',
'ImageBitmapSource.h',
'ImageData.h',
'TextMetrics.h',
'WebGLVertexArrayObject.h',
@ -44,6 +46,7 @@ UNIFIED_SOURCES += [
'CanvasUtils.cpp',
'DocumentRendererChild.cpp',
'DocumentRendererParent.cpp',
'ImageBitmap.cpp',
'ImageData.cpp',
]
@ -146,6 +149,7 @@ include('/ipc/chromium/chromium-config.mozbuild')
FINAL_LIBRARY = 'xul'
LOCAL_INCLUDES += [
'../workers',
'/dom/base',
'/dom/html',
'/dom/svg',
@ -156,6 +160,7 @@ LOCAL_INCLUDES += [
'/layout/generic',
'/layout/style',
'/layout/xul',
'/media/libyuv/include',
]
CXXFLAGS += CONFIG['MOZ_CAIRO_CFLAGS']

View File

@ -749,9 +749,12 @@ GetCanvasContextType(const nsAString& str, CanvasContextType* const out_type)
static already_AddRefed<nsICanvasRenderingContextInternal>
CreateContextForCanvas(CanvasContextType contextType, HTMLCanvasElement* canvas)
{
MOZ_ASSERT(contextType != CanvasContextType::NoContext);
nsRefPtr<nsICanvasRenderingContextInternal> ret;
switch (contextType) {
case CanvasContextType::NoContext:
break;
case CanvasContextType::Canvas2D:
Telemetry::Accumulate(Telemetry::CANVAS_2D_USED, 1);
ret = new CanvasRenderingContext2D();

View File

@ -36,6 +36,7 @@ class HTMLCanvasPrintState;
class PrintCallback;
enum class CanvasContextType : uint8_t {
NoContext,
Canvas2D,
WebGL1,
WebGL2
@ -262,6 +263,10 @@ public:
void ResetPrintCallback();
HTMLCanvasElement* GetOriginalCanvas();
CanvasContextType GetCurrentContextType() {
return mCurrentContextType;
}
};
class HTMLCanvasPrintState final : public nsWrapperCache

View File

@ -0,0 +1,32 @@
/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/.
*
* The origin of this IDL file is
* https://html.spec.whatwg.org/multipage/webappapis.html#images
*/
typedef (HTMLImageElement or
HTMLVideoElement or
HTMLCanvasElement or
Blob or
ImageData or
CanvasRenderingContext2D or
ImageBitmap) ImageBitmapSource;
[Exposed=(Window,Worker)]
interface ImageBitmap {
[Constant]
readonly attribute unsigned long width;
[Constant]
readonly attribute unsigned long height;
};
[NoInterfaceObject, Exposed=(Window,Worker)]
interface ImageBitmapFactories {
[Throws]
Promise<ImageBitmap> createImageBitmap(ImageBitmapSource aImage);
[Throws]
Promise<ImageBitmap> createImageBitmap(ImageBitmapSource aImage, long aSx, long aSy, long aSw, long aSh);
};

View File

@ -483,3 +483,4 @@ interface ChromeWindow {
Window implements ChromeWindow;
Window implements GlobalFetch;
Window implements ImageBitmapFactories;

View File

@ -48,6 +48,7 @@ WorkerGlobalScope implements WindowTimers;
WorkerGlobalScope implements WindowBase64;
WorkerGlobalScope implements GlobalFetch;
WorkerGlobalScope implements IDBEnvironment;
WorkerGlobalScope implements ImageBitmapFactories;
// Not implemented yet: bug 1072107.
// WorkerGlobalScope implements FontFaceSource;

View File

@ -259,6 +259,7 @@ WEBIDL_FILES = [
'IDBRequest.webidl',
'IDBTransaction.webidl',
'IDBVersionChangeEvent.webidl',
'ImageBitmap.webidl',
'ImageCapture.webidl',
'ImageData.webidl',
'ImageDocument.webidl',

View File

@ -13,6 +13,7 @@
#include "mozilla/dom/DedicatedWorkerGlobalScopeBinding.h"
#include "mozilla/dom/Fetch.h"
#include "mozilla/dom/FunctionBinding.h"
#include "mozilla/dom/ImageBitmap.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/PromiseWorkerProxy.h"
#include "mozilla/dom/ServiceWorkerGlobalScopeBinding.h"
@ -386,6 +387,21 @@ WorkerGlobalScope::GetIndexedDB(ErrorResult& aErrorResult)
return indexedDB.forget();
}
already_AddRefed<Promise>
WorkerGlobalScope::CreateImageBitmap(const ImageBitmapSource& aImage,
ErrorResult& aRv)
{
return ImageBitmap::Create(this, aImage, Nothing(), aRv);
}
already_AddRefed<Promise>
WorkerGlobalScope::CreateImageBitmap(const ImageBitmapSource& aImage,
int32_t aSx, int32_t aSy, int32_t aSw, int32_t aSh,
ErrorResult& aRv)
{
return ImageBitmap::Create(this, aImage, Some(gfx::IntRect(aSx, aSy, aSw, aSh)), aRv);
}
DedicatedWorkerGlobalScope::DedicatedWorkerGlobalScope(WorkerPrivate* aWorkerPrivate)
: WorkerGlobalScope(aWorkerPrivate)
{

View File

@ -12,6 +12,7 @@
#include "mozilla/dom/Headers.h"
#include "mozilla/dom/RequestBinding.h"
#include "nsWeakReference.h"
#include "mozilla/dom/ImageBitmapSource.h"
namespace mozilla {
namespace dom {
@ -153,6 +154,14 @@ public:
already_AddRefed<cache::CacheStorage>
GetCaches(ErrorResult& aRv);
already_AddRefed<Promise>
CreateImageBitmap(const ImageBitmapSource& aImage, ErrorResult& aRv);
already_AddRefed<Promise>
CreateImageBitmap(const ImageBitmapSource& aImage,
int32_t aSx, int32_t aSy, int32_t aSw, int32_t aSh,
ErrorResult& aRv);
};
class DedicatedWorkerGlobalScope final : public WorkerGlobalScope