blob: bdc66c79b9a726db688957128346b8b0fa89a4f5 [file] [log] [blame]
bungeman@google.come8f05922012-08-16 16:13:40 +00001/*
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
mtklein7b274c72015-02-03 13:38:58 -080013#include "SkMutex.h"
bungeman@google.come8f05922012-08-16 16:13:40 +000014#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 */
bungeman@google.com6cab1a42013-05-29 13:43:31 +000023class SkDWriteFontFileStream : public SkStreamMemory {
bungeman@google.come8f05922012-08-16 16:13:40 +000024public:
25 explicit SkDWriteFontFileStream(IDWriteFontFileStream* fontFileStream);
Chris Dalton1ef80942017-12-04 12:01:30 -070026 ~SkDWriteFontFileStream() override;
rmistry@google.comd6176b02012-08-23 18:14:13 +000027
mtklein36352bf2015-03-25 18:17:31 -070028 size_t read(void* buffer, size_t size) override;
29 bool isAtEnd() const override;
30 bool rewind() override;
mtklein36352bf2015-03-25 18:17:31 -070031 size_t getPosition() const override;
32 bool seek(size_t position) override;
33 bool move(long offset) override;
mtklein36352bf2015-03-25 18:17:31 -070034 size_t getLength() const override;
35 const void* getMemoryBase() override;
bungeman@google.come8f05922012-08-16 16:13:40 +000036
Mike Reed98c5d922017-09-15 21:39:47 -040037 std::unique_ptr<SkDWriteFontFileStream> duplicate() const {
38 return std::unique_ptr<SkDWriteFontFileStream>(this->onDuplicate());
39 }
40 std::unique_ptr<SkDWriteFontFileStream> fork() const {
41 return std::unique_ptr<SkDWriteFontFileStream>(this->onFork());
42 }
Mike Reed98c5d922017-09-15 21:39:47 -040043
bungeman@google.come8f05922012-08-16 16:13:40 +000044private:
Mike Reed98c5d922017-09-15 21:39:47 -040045 SkDWriteFontFileStream* onDuplicate() const override;
46 SkDWriteFontFileStream* onFork() const override;
Mike Reed98c5d922017-09-15 21:39:47 -040047
bungeman@google.come8f05922012-08-16 16:13:40 +000048 SkTScopedComPtr<IDWriteFontFileStream> fFontFileStream;
49 size_t fPos;
50 const void* fLockedMemory;
51 void* fFragmentLock;
52};
53
54/**
55 * An IDWriteFontFileStream backed by an SkStream.
56 * This allows DirectWrite to read an SkStream.
57 */
58class SkDWriteFontFileStreamWrapper : public IDWriteFontFileStream {
59public:
60 // IUnknown methods
61 virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** ppvObject);
62 virtual ULONG STDMETHODCALLTYPE AddRef();
63 virtual ULONG STDMETHODCALLTYPE Release();
64
65 // IDWriteFontFileStream methods
66 virtual HRESULT STDMETHODCALLTYPE ReadFileFragment(
67 void const** fragmentStart,
68 UINT64 fileOffset,
69 UINT64 fragmentSize,
70 void** fragmentContext);
71
72 virtual void STDMETHODCALLTYPE ReleaseFileFragment(void* fragmentContext);
73 virtual HRESULT STDMETHODCALLTYPE GetFileSize(UINT64* fileSize);
74 virtual HRESULT STDMETHODCALLTYPE GetLastWriteTime(UINT64* lastWriteTime);
75
bungeman0babd3c2015-02-18 11:01:05 -080076 static HRESULT Create(SkStreamAsset* stream,
77 SkDWriteFontFileStreamWrapper** streamFontFileStream);
bungeman@google.come8f05922012-08-16 16:13:40 +000078
79private:
bungeman0babd3c2015-02-18 11:01:05 -080080 explicit SkDWriteFontFileStreamWrapper(SkStreamAsset* stream);
bungemand7f846b2014-06-19 11:26:59 -070081 virtual ~SkDWriteFontFileStreamWrapper() { }
bungeman@google.come8f05922012-08-16 16:13:40 +000082
83 ULONG fRefCount;
Ben Wagner145dbcd2016-11-03 14:40:50 -040084 std::unique_ptr<SkStreamAsset> fStream;
bungeman@google.come8f05922012-08-16 16:13:40 +000085 SkMutex fStreamMutex;
86};
87#endif