1 /*
2  * Copyright 2012 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SkDWriteFontFileStream_DEFINED
9 #define SkDWriteFontFileStream_DEFINED
10 
11 #include "SkTypes.h"
12 
13 #include "SkMutex.h"
14 #include "SkStream.h"
15 #include "SkTScopedComPtr.h"
16 
17 #include <dwrite.h>
18 
19 /**
20  *  An SkStream backed by an IDWriteFontFileStream.
21  *  This allows Skia code to read an IDWriteFontFileStream.
22  */
23 class SkDWriteFontFileStream : public SkStreamMemory {
24 public:
25     explicit SkDWriteFontFileStream(IDWriteFontFileStream* fontFileStream);
26     virtual ~SkDWriteFontFileStream();
27 
28     size_t read(void* buffer, size_t size) override;
29     bool isAtEnd() const override;
30     bool rewind() override;
31     SkDWriteFontFileStream* duplicate() const override;
32     size_t getPosition() const override;
33     bool seek(size_t position) override;
34     bool move(long offset) override;
35     SkDWriteFontFileStream* fork() const override;
36     size_t getLength() const override;
37     const void* getMemoryBase() override;
38 
39 private:
40     SkTScopedComPtr<IDWriteFontFileStream> fFontFileStream;
41     size_t fPos;
42     const void* fLockedMemory;
43     void* fFragmentLock;
44 };
45 
46 /**
47  *  An IDWriteFontFileStream backed by an SkStream.
48  *  This allows DirectWrite to read an SkStream.
49  */
50 class SkDWriteFontFileStreamWrapper : public IDWriteFontFileStream {
51 public:
52     // IUnknown methods
53     virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** ppvObject);
54     virtual ULONG STDMETHODCALLTYPE AddRef();
55     virtual ULONG STDMETHODCALLTYPE Release();
56 
57     // IDWriteFontFileStream methods
58     virtual HRESULT STDMETHODCALLTYPE ReadFileFragment(
59         void const** fragmentStart,
60         UINT64 fileOffset,
61         UINT64 fragmentSize,
62         void** fragmentContext);
63 
64     virtual void STDMETHODCALLTYPE ReleaseFileFragment(void* fragmentContext);
65     virtual HRESULT STDMETHODCALLTYPE GetFileSize(UINT64* fileSize);
66     virtual HRESULT STDMETHODCALLTYPE GetLastWriteTime(UINT64* lastWriteTime);
67 
68     static HRESULT Create(SkStreamAsset* stream,
69                           SkDWriteFontFileStreamWrapper** streamFontFileStream);
70 
71 private:
72     explicit SkDWriteFontFileStreamWrapper(SkStreamAsset* stream);
~SkDWriteFontFileStreamWrapper()73     virtual ~SkDWriteFontFileStreamWrapper() { }
74 
75     ULONG fRefCount;
76     SkAutoTDelete<SkStreamAsset> fStream;
77     SkMutex fStreamMutex;
78 };
79 #endif
80