1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #include "gfxWindowsSurface.h"
7 #include "gfxContext.h"
8 #include "gfxPlatform.h"
9 #include "mozilla/gfx/2D.h"
10 #include "mozilla/gfx/HelpersCairo.h"
11 #include "mozilla/gfx/Logging.h"
12 
13 #include "cairo.h"
14 #include "cairo-win32.h"
15 
16 #include "nsString.h"
17 
gfxWindowsSurface(HDC dc,uint32_t flags)18 gfxWindowsSurface::gfxWindowsSurface(HDC dc, uint32_t flags) :
19     mOwnsDC(false), mDC(dc), mWnd(nullptr)
20 {
21     InitWithDC(flags);
22 }
23 
gfxWindowsSurface(IDirect3DSurface9 * surface,uint32_t flags)24 gfxWindowsSurface::gfxWindowsSurface(IDirect3DSurface9 *surface, uint32_t flags) :
25     mOwnsDC(false), mDC(0), mWnd(nullptr)
26 {
27     cairo_surface_t *surf = cairo_win32_surface_create_with_d3dsurface9(surface);
28     Init(surf);
29 }
30 
31 
32 void
MakeInvalid(mozilla::gfx::IntSize & size)33 gfxWindowsSurface::MakeInvalid(mozilla::gfx::IntSize& size)
34 {
35     size = mozilla::gfx::IntSize(-1, -1);
36 }
37 
gfxWindowsSurface(const mozilla::gfx::IntSize & realSize,gfxImageFormat imageFormat)38 gfxWindowsSurface::gfxWindowsSurface(const mozilla::gfx::IntSize& realSize, gfxImageFormat imageFormat) :
39     mOwnsDC(false), mWnd(nullptr)
40 {
41     mozilla::gfx::IntSize size(realSize);
42     if (!mozilla::gfx::Factory::CheckSurfaceSize(size))
43         MakeInvalid(size);
44 
45     cairo_format_t cformat = GfxFormatToCairoFormat(imageFormat);
46     cairo_surface_t *surf =
47         cairo_win32_surface_create_with_dib(cformat, size.width, size.height);
48 
49     Init(surf);
50 
51     if (CairoStatus() == CAIRO_STATUS_SUCCESS) {
52         mDC = cairo_win32_surface_get_dc(CairoSurface());
53         RecordMemoryUsed(size.width * size.height * 4 + sizeof(gfxWindowsSurface));
54     } else {
55         mDC = nullptr;
56     }
57 }
58 
gfxWindowsSurface(cairo_surface_t * csurf)59 gfxWindowsSurface::gfxWindowsSurface(cairo_surface_t *csurf) :
60     mOwnsDC(false), mWnd(nullptr)
61 {
62     if (cairo_surface_status(csurf) == 0)
63         mDC = cairo_win32_surface_get_dc(csurf);
64     else
65         mDC = nullptr;
66 
67     MOZ_ASSERT(cairo_surface_get_type(csurf) != CAIRO_SURFACE_TYPE_WIN32_PRINTING);
68 
69     Init(csurf, true);
70 }
71 
72 void
InitWithDC(uint32_t flags)73 gfxWindowsSurface::InitWithDC(uint32_t flags)
74 {
75     if (flags & FLAG_IS_TRANSPARENT) {
76         Init(cairo_win32_surface_create_with_alpha(mDC));
77     } else {
78         Init(cairo_win32_surface_create(mDC));
79     }
80 }
81 
82 already_AddRefed<gfxASurface>
CreateSimilarSurface(gfxContentType aContent,const mozilla::gfx::IntSize & aSize)83 gfxWindowsSurface::CreateSimilarSurface(gfxContentType aContent,
84                                         const mozilla::gfx::IntSize& aSize)
85 {
86     if (!mSurface || !mSurfaceValid) {
87         return nullptr;
88     }
89 
90     cairo_surface_t *surface;
91     if (GetContentType() == gfxContentType::COLOR_ALPHA) {
92         // When creating a similar surface to a transparent surface, ensure
93         // the new surface uses a DIB. cairo_surface_create_similar won't
94         // use  a DIB for a gfxContentType::COLOR surface if this surface doesn't
95         // have a DIB (e.g. if we're a transparent window surface). But
96         // we need a DIB to perform well if the new surface is composited into
97         // a surface that's the result of create_similar(gfxContentType::COLOR_ALPHA)
98         // (e.g. a backbuffer for the window) --- that new surface *would*
99         // have a DIB.
100         gfxImageFormat gformat =
101             gfxPlatform::GetPlatform()->OptimalFormatForContent(aContent);
102         cairo_format_t cformat = GfxFormatToCairoFormat(gformat);
103         surface = cairo_win32_surface_create_with_dib(cformat, aSize.width,
104                                                       aSize.height);
105     } else {
106         surface =
107           cairo_surface_create_similar(mSurface, (cairo_content_t)(int)aContent,
108                                        aSize.width, aSize.height);
109     }
110 
111     if (cairo_surface_status(surface)) {
112         cairo_surface_destroy(surface);
113         return nullptr;
114     }
115 
116     RefPtr<gfxASurface> result = Wrap(surface, aSize);
117     cairo_surface_destroy(surface);
118     return result.forget();
119 }
120 
~gfxWindowsSurface()121 gfxWindowsSurface::~gfxWindowsSurface()
122 {
123     if (mOwnsDC) {
124         if (mWnd)
125             ::ReleaseDC(mWnd, mDC);
126         else
127             ::DeleteDC(mDC);
128     }
129 }
130 
131 HDC
GetDC()132 gfxWindowsSurface::GetDC()
133 {
134     return cairo_win32_surface_get_dc (CairoSurface());
135 }
136 
137 already_AddRefed<gfxImageSurface>
GetAsImageSurface()138 gfxWindowsSurface::GetAsImageSurface()
139 {
140     if (!mSurfaceValid) {
141         NS_WARNING ("GetImageSurface on an invalid (null) surface; who's calling this without checking for surface errors?");
142         return nullptr;
143     }
144 
145     NS_ASSERTION(CairoSurface() != nullptr, "CairoSurface() shouldn't be nullptr when mSurfaceValid is TRUE!");
146 
147     cairo_surface_t *isurf = cairo_win32_surface_get_image(CairoSurface());
148     if (!isurf)
149         return nullptr;
150 
151     RefPtr<gfxImageSurface> result = gfxASurface::Wrap(isurf).downcast<gfxImageSurface>();
152     result->SetOpaqueRect(GetOpaqueRect());
153 
154     return result.forget();
155 }
156 
157 const mozilla::gfx::IntSize
GetSize() const158 gfxWindowsSurface::GetSize() const
159 {
160     if (!mSurfaceValid) {
161         NS_WARNING ("GetImageSurface on an invalid (null) surface; who's calling this without checking for surface errors?");
162         return mozilla::gfx::IntSize(-1, -1);
163     }
164 
165     NS_ASSERTION(mSurface != nullptr, "CairoSurface() shouldn't be nullptr when mSurfaceValid is TRUE!");
166 
167     return mozilla::gfx::IntSize(cairo_win32_surface_get_width(mSurface),
168                       cairo_win32_surface_get_height(mSurface));
169 }
170