1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Helper functions that Direct3D 9Ex code a little easier to work with for
6// the ui/surface code.
7
8#ifndef UI_SURFACE_D3D9_UTILS_WIN_H_
9#define UI_SURFACE_D3D9_UTILS_WIN_H_
10
11#include <d3d9.h>
12
13#include "base/basictypes.h"
14#include "base/win/scoped_comptr.h"
15#include "ui/surface/surface_export.h"
16
17namespace base {
18class ScopedNativeLibrary;
19}
20
21namespace gfx {
22class Size;
23}
24
25namespace ui_surface_d3d9_utils {
26
27// Visible for testing. Loads the Direct3D9 library. Returns true on success.
28SURFACE_EXPORT
29bool LoadD3D9(base::ScopedNativeLibrary* storage);
30
31// Visible for testing. Creates a Direct3D9 device suitable for use with the
32// accelerated surface code. Returns true on success.
33SURFACE_EXPORT
34bool CreateDevice(const base::ScopedNativeLibrary& d3d_module,
35                  uint64 adapter_luid,
36                  D3DDEVTYPE device_type,
37                  uint32 presentation_interval,
38                  IDirect3DDevice9Ex** device);
39
40// Calls the Vista+ (WDDM1.0) variant of CreateTexture that semantically opens a
41// texture allocated as shared. In this way textures allocated by another
42// process can be used by a D3D context in this process. The shared texture is
43// identified by its surface handle. The resulting texture is written into
44// |opened_texture|.
45//
46// Returns true on success.
47SURFACE_EXPORT
48bool OpenSharedTexture(IDirect3DDevice9* device,
49                       int64 surface_handle,
50                       const gfx::Size& size,
51                       IDirect3DTexture9** opened_texture);
52
53// Ensures that |surface| is a lockable surface of a specified |size|. If
54// |*surface| is non-null and has dimensions that match |size|, it is reused.
55// Otherwise, a new resource is created and the old one (if any) is freed.
56//
57// Returns true on success.
58SURFACE_EXPORT
59bool CreateOrReuseLockableSurface(
60    IDirect3DDevice9* device,
61    const gfx::Size& size,
62    base::win::ScopedComPtr<IDirect3DSurface9>* surface);
63
64// Ensures that |texture| is a render target texture of a specified |size|. If
65// |*texture| is non-null and has dimensions that match |size|, it is reused.
66// Otherwise, a new resource is created and the old one (if any) is freed.
67//
68// A reference to level 0 of the resulting texture is placed into
69// |render_target|.
70//
71// Returns true on success.
72SURFACE_EXPORT
73bool CreateOrReuseRenderTargetTexture(
74    IDirect3DDevice9* device,
75    const gfx::Size& size,
76    base::win::ScopedComPtr<IDirect3DTexture9>* texture,
77    IDirect3DSurface9** render_target);
78
79SURFACE_EXPORT
80gfx::Size GetSize(IDirect3DTexture9* texture);
81
82SURFACE_EXPORT
83gfx::Size GetSize(IDirect3DSurface9* surface);
84
85}  // namespace ui_surface_d3d9_utils
86
87#endif  // UI_SURFACE_D3D9_UTILS_WIN_H_
88