summaryrefslogtreecommitdiffstats
path: root/win8/metro_driver/print_document_source.cc
blob: 91842b78c68737bbdcab8e60627d6d0a80211683 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "stdafx.h"
#include "win8/metro_driver/print_document_source.h"

#include <windows.graphics.display.h>

#include "base/logging.h"
#include "base/safe_numerics.h"


namespace {

class D2DFactoryAutoLock {
 public:
  explicit D2DFactoryAutoLock(ID2D1Factory* d2d_factory) {
    HRESULT hr = d2d_factory->QueryInterface(IID_PPV_ARGS(&d2d_multithread_));
    if (d2d_multithread_.Get())
      d2d_multithread_->Enter();
    else
      NOTREACHED() << "Failed to QI for ID2D1Multithread " << std::hex << hr;
  }

  ~D2DFactoryAutoLock() {
    if (d2d_multithread_.Get())
      d2d_multithread_->Leave();
  }

 private:
  mswr::ComPtr<ID2D1Multithread> d2d_multithread_;
};

// TODO(mad): remove once we don't run mixed SDK/OS anymore.
const GUID kOldPackageTargetGuid =
    {0xfb2a33c0, 0x8c35, 0x465f,
      {0xbe, 0xd5, 0x9f, 0x36, 0x89, 0x51, 0x77, 0x52}};
const GUID kNewPackageTargetGuid =
    {0x1a6dd0ad, 0x1e2a, 0x4e99,
      {0xa5, 0xba, 0x91, 0xf1, 0x78, 0x18, 0x29, 0x0e}};


}  // namespace

namespace metro_driver {

PrintDocumentSource::PrintDocumentSource()
    : page_count_ready_(true, false),
      parent_lock_(NULL),
      height_(0),
      width_(0),
      dpi_(96),
      aborted_(false),
      using_old_preview_interface_(false) {
}

HRESULT PrintDocumentSource::RuntimeClassInitialize(
    const DirectXContext& directx_context,
    base::Lock* parent_lock) {
  DCHECK(parent_lock != NULL);
  DCHECK(directx_context.d2d_context.Get() != NULL);
  DCHECK(directx_context.d2d_device.Get() != NULL);
  DCHECK(directx_context.d2d_factory.Get() != NULL);
  DCHECK(directx_context.d3d_device.Get() != NULL);
  DCHECK(directx_context.wic_factory.Get() != NULL);
  directx_context_ = directx_context;

  // No other method can be called before RuntimeClassInitialize which is called
  // during the construction via mswr::MakeAndInitialize(), so it's safe for all
  // other methods to use the parent_lock_ without checking if it's NULL.
  DCHECK(parent_lock_ == NULL);
  parent_lock_ = parent_lock;

  return S_OK;
}

void PrintDocumentSource::Abort() {
  base::AutoLock lock(*parent_lock_);
  aborted_ = true;
  if (page_count_ready_.IsSignaled()) {
    pages_.clear();
    for (size_t i = 0; i < pages_ready_state_.size(); ++i)
      pages_ready_state_[i]->Broadcast();
  } else {
    DCHECK(pages_.empty() && pages_ready_state_.empty());
  }
}

STDMETHODIMP PrintDocumentSource::GetPreviewPageCollection(
    IPrintDocumentPackageTarget* package_target,
    IPrintPreviewPageCollection** page_collection) {
  DVLOG(1) << __FUNCTION__;
  DCHECK(package_target != NULL);
  DCHECK(page_collection != NULL);

  HRESULT hr = package_target->GetPackageTarget(
      __uuidof(IPrintPreviewDxgiPackageTarget),
      IID_PPV_ARGS(&dxgi_preview_target_));
  if (FAILED(hr)) {
    // TODO(mad): remove once we don't run mixed SDK/OS anymore.
    // The IID changed from one version of the SDK to another, so try the other
    // one in case we are running a build from a different SDK than the one
    // related to the OS version we are running.
    GUID package_target_uuid = kNewPackageTargetGuid;
    if (package_target_uuid == __uuidof(IPrintPreviewDxgiPackageTarget)) {
      package_target_uuid = kOldPackageTargetGuid;
      using_old_preview_interface_ = true;
    }
    hr = package_target->GetPackageTarget(package_target_uuid,
                                          package_target_uuid,
                                          &dxgi_preview_target_);
    if (FAILED(hr)) {
      LOG(ERROR) << "Failed to get IPrintPreviewDXGIPackageTarget " << std::hex
                 << hr;
      return hr;
    }
  } else {
    using_old_preview_interface_ = (__uuidof(IPrintPreviewDxgiPackageTarget) ==
                                    kOldPackageTargetGuid);
  }

  mswr::ComPtr<IPrintPreviewPageCollection> preview_page_collection;
  mswr::ComPtr<PrintDocumentSource> print_document_source(this);
  hr = print_document_source.As(&preview_page_collection);
  if (FAILED(hr)) {
    LOG(ERROR) << "Failed to get preview_page_collection " << std::hex << hr;
    return hr;
  }

  hr = preview_page_collection.CopyTo(page_collection);
  if (FAILED(hr)) {
    LOG(ERROR) << "Failed to copy preview_page_collection " << std::hex << hr;
    return hr;
  }
  return hr;
}

STDMETHODIMP PrintDocumentSource::MakeDocument(
    IInspectable* options,
    IPrintDocumentPackageTarget* package_target) {
  DVLOG(1) << __FUNCTION__;
  DCHECK(options != NULL);
  DCHECK(package_target != NULL);

  mswr::ComPtr<wingfx::Printing::IPrintTaskOptionsCore> print_task_options;
  HRESULT hr = options->QueryInterface(
      wingfx::Printing::IID_IPrintTaskOptionsCore,
      reinterpret_cast<void**>(print_task_options.GetAddressOf()));
  if (FAILED(hr)) {
    LOG(ERROR) << "Failed to QI for IPrintTaskOptionsCore " << std::hex << hr;
    return hr;
  }

  // Use the first page's description for the whole document. Page numbers
  // are 1-based in this context.
  // TODO(mad): Check if it would be useful to use per page descriptions.
  wingfx::Printing::PrintPageDescription page_desc = {};
  hr = print_task_options->GetPageDescription(1 /* page */, &page_desc);
  if (FAILED(hr)) {
    LOG(ERROR) << "Failed to GetPageDescription " << std::hex << hr;
    return hr;
  }

  D2D1_PRINT_CONTROL_PROPERTIES print_control_properties;
  if (page_desc.DpiX > page_desc.DpiY)
    print_control_properties.rasterDPI = static_cast<float>(page_desc.DpiY);
  else
    print_control_properties.rasterDPI = static_cast<float>(page_desc.DpiX);

  // Color space for vector graphics in D2D print control.
  print_control_properties.colorSpace = D2D1_COLOR_SPACE_SRGB;
  print_control_properties.fontSubset = D2D1_PRINT_FONT_SUBSET_MODE_DEFAULT;

  mswr::ComPtr<ID2D1PrintControl> print_control;
  hr = directx_context_.d2d_device->CreatePrintControl(
      directx_context_.wic_factory.Get(),
      package_target,
      print_control_properties,
      print_control.GetAddressOf());
  if (FAILED(hr)) {
    LOG(ERROR) << "Failed to CreatePrintControl " << std::hex << hr;
    return hr;
  }

  D2D1_SIZE_F page_size = D2D1::SizeF(page_desc.PageSize.Width,
                                      page_desc.PageSize.Height);

  // Wait for the number of pages to be available.
  // If an abort occured, we'll get 0 and won't enter the loop below.
  size_t page_count = WaitAndGetPageCount();

  mswr::ComPtr<ID2D1GdiMetafile> gdi_metafile;
  for (size_t page = 0; page < page_count; ++page) {
    gdi_metafile.Reset();
    hr = WaitAndGetPage(page, gdi_metafile.GetAddressOf());
    LOG_IF(ERROR, FAILED(hr)) << "Failed to get page's metafile " << std::hex
                              << hr;
    // S_FALSE means we got aborted.
    if (hr == S_FALSE || FAILED(hr))
      break;
    hr = PrintPage(print_control.Get(), gdi_metafile.Get(), page_size);
    if (FAILED(hr))
      break;
  }

  HRESULT close_hr = print_control->Close();
  if (FAILED(close_hr) && SUCCEEDED(hr))
    return close_hr;
  else
    return hr;
}

STDMETHODIMP PrintDocumentSource::Paginate(uint32 page,
                                           IInspectable* options) {
  DVLOG(1) << __FUNCTION__ << ", page = " << page;
  DCHECK(options != NULL);
  // GetPreviewPageCollection must have been successfuly called.
  DCHECK(dxgi_preview_target_.Get() != NULL);

  // Get print settings from PrintTaskOptions for preview, such as page
  // description, which contains page size, imageable area, DPI.
  // TODO(mad): obtain other print settings in the same way, such as ColorMode,
  // NumberOfCopies, etc...
  mswr::ComPtr<wingfx::Printing::IPrintTaskOptionsCore> print_options;
  HRESULT hr = options->QueryInterface(
      wingfx::Printing::IID_IPrintTaskOptionsCore,
      reinterpret_cast<void**>(print_options.GetAddressOf()));
  if (FAILED(hr)) {
    LOG(ERROR) << "Failed to QI for IPrintTaskOptionsCore " << std::hex << hr;
    return hr;
  }

  wingfx::Printing::PrintPageDescription page_desc = {};
  hr = print_options->GetPageDescription(1 /* page */, &page_desc);
  if (FAILED(hr)) {
    LOG(ERROR) << "Failed to GetPageDescription " << std::hex << hr;
    return hr;
  }

  width_ = page_desc.PageSize.Width;
  height_ = page_desc.PageSize.Height;

  hr = dxgi_preview_target_->InvalidatePreview();
  if (FAILED(hr)) {
    LOG(ERROR) << "Failed to InvalidatePreview " << std::hex << hr;
    return hr;
  }

  size_t page_count = WaitAndGetPageCount();
  // A page_count of 0 means abort...
  if (page_count == 0)
    return S_FALSE;
  hr = dxgi_preview_target_->SetJobPageCount(
           PageCountType::FinalPageCount,
           base::checked_numeric_cast<UINT32>(page_count));
  if (FAILED(hr)) {
    LOG(ERROR) << "Failed to SetJobPageCount " << std::hex << hr;
    return hr;
  }
  return hr;
}

STDMETHODIMP PrintDocumentSource::MakePage(uint32 job_page,
                                           float width,
                                           float height) {
  DVLOG(1) << __FUNCTION__ << ", width: " << width << ", height: " << height
          << ", job_page: " << job_page;
  DCHECK(width > 0 && height > 0);
  // Paginate must have been called before this.
  if (width_ <= 0.0 || height_ <= 0.0)
    return S_FALSE;

  // When job_page is JOB_PAGE_APPLICATION_DEFINED, it means a new preview
  // begins. TODO(mad): Double check if we need to cancel pending resources.
  if (job_page == JOB_PAGE_APPLICATION_DEFINED)
    job_page = 1;

  winfoundtn::Size preview_size;
  preview_size.Width  = width;
  preview_size.Height = height;
  float scale = width_ / width;

  mswr::ComPtr<ID2D1Factory> factory;
  directx_context_.d2d_device->GetFactory(&factory);

  mswr::ComPtr<ID2D1GdiMetafile> gdi_metafile;
  HRESULT hr = WaitAndGetPage(job_page - 1, gdi_metafile.GetAddressOf());
  LOG_IF(ERROR, FAILED(hr)) << "Failed to get page's metafile " << std::hex
                            << hr;
  // Again, S_FALSE means we got aborted.
  if (hr == S_FALSE || FAILED(hr))
    return hr;

  // We are accessing D3D resources directly without D2D's knowledge, so we
  // must manually acquire the D2D factory lock.
  D2DFactoryAutoLock factory_lock(directx_context_.d2d_factory.Get());

  CD3D11_TEXTURE2D_DESC texture_desc(
      DXGI_FORMAT_B8G8R8A8_UNORM,
      static_cast<UINT32>(ceil(width  * dpi_ / 96)),
      static_cast<UINT32>(ceil(height * dpi_ / 96)),
      1,
      1,
      D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE
      );
  mswr::ComPtr<ID3D11Texture2D> texture;
  hr = directx_context_.d3d_device->CreateTexture2D(
      &texture_desc, NULL, &texture);
  if (FAILED(hr)) {
    LOG(ERROR) << "Failed to create a 2D texture " << std::hex << hr;
    return hr;
  }

  mswr::ComPtr<IDXGISurface> dxgi_surface;
  hr = texture.As<IDXGISurface>(&dxgi_surface);
  if (FAILED(hr)) {
    LOG(ERROR) << "Failed to QI for IDXGISurface " << std::hex << hr;
    return hr;
  }

  // D2D device contexts are stateful, and hence a unique device context must
  // be used on each call.
  mswr::ComPtr<ID2D1DeviceContext> d2d_context;
  hr = directx_context_.d2d_device->CreateDeviceContext(
      D2D1_DEVICE_CONTEXT_OPTIONS_NONE, &d2d_context);

  d2d_context->SetDpi(dpi_, dpi_);

  mswr::ComPtr<ID2D1Bitmap1> d2dSurfaceBitmap;
  hr = d2d_context->CreateBitmapFromDxgiSurface(dxgi_surface.Get(),
                                                NULL,  // default properties.
                                                &d2dSurfaceBitmap);
  if (FAILED(hr)) {
    LOG(ERROR) << "Failed to CreateBitmapFromDxgiSurface " << std::hex << hr;
    return hr;
  }

  d2d_context->SetTarget(d2dSurfaceBitmap.Get());
  d2d_context->BeginDraw();
  d2d_context->Clear();
  d2d_context->SetTransform(D2D1::Matrix3x2F(1/scale, 0, 0, 1/scale, 0, 0));
  d2d_context->DrawGdiMetafile(gdi_metafile.Get());

  hr = d2d_context->EndDraw();
  if (FAILED(hr)) {
    LOG(ERROR) << "Failed to EndDraw " << std::hex << hr;
    return hr;
  }

// TODO(mad): remove once we don't run mixed SDK/OS anymore.
#ifdef __IPrintPreviewDxgiPackageTarget_FWD_DEFINED__
  FLOAT dpi = dpi_;
  if (using_old_preview_interface_) {
    // We compiled with the new API but run on the old OS, so we must cheat
    // and send something that looks like a float but has a UINT32 value.
    *reinterpret_cast<UINT32*>(&dpi) = static_cast<UINT32>(dpi_);
  }
#else
  UINT32 dpi = static_cast<UINT32>(dpi_);
  if (!using_old_preview_interface_) {
    // We compiled with the old API but run on the new OS, so we must cheat
    // and send something that looks like a UINT32 but has a float value.
    *reinterpret_cast<FLOAT*>(&dpi) = dpi_;
  }
#endif  // __IPrintPreviewDxgiPackageTarget_FWD_DEFINED__
  hr = dxgi_preview_target_->DrawPage(job_page, dxgi_surface.Get(), dpi, dpi);
  if (FAILED(hr)) {
    LOG(ERROR) << "Failed to DrawPage " << std::hex << hr;
    return hr;
  }
  return hr;
}

void PrintDocumentSource::ResetDpi(float dpi) {
  {
    base::AutoLock lock(*parent_lock_);
    if (dpi == dpi_)
      return;
    dpi_ = dpi;
  }
  directx_context_.d2d_context->SetDpi(dpi, dpi);
}

void PrintDocumentSource::SetPageCount(size_t page_count) {
  DCHECK(page_count > 0);
  {
    base::AutoLock lock(*parent_lock_);
    DCHECK(!page_count_ready_.IsSignaled());
    DCHECK(pages_.empty() && pages_ready_state_.empty());

    pages_.resize(page_count);
    pages_ready_state_.resize(page_count);

    for (size_t i = 0; i < page_count; ++i)
      pages_ready_state_[i].reset(new base::ConditionVariable(parent_lock_));
  }
  page_count_ready_.Signal();
}

void PrintDocumentSource::AddPage(size_t page_number,
                                  IStream* metafile_stream) {
  DCHECK(metafile_stream != NULL);
  base::AutoLock lock(*parent_lock_);

  DCHECK(page_count_ready_.IsSignaled());
  DCHECK(page_number < pages_.size());

  pages_[page_number] = metafile_stream;
  pages_ready_state_[page_number]->Signal();
}

HRESULT PrintDocumentSource::PrintPage(ID2D1PrintControl* print_control,
                                       ID2D1GdiMetafile* gdi_metafile,
                                       D2D1_SIZE_F page_size) {
  DVLOG(1) << __FUNCTION__ << ", page_size: (" << page_size.width << ", "
          << page_size.height << ")";
  DCHECK(print_control != NULL);
  DCHECK(gdi_metafile != NULL);

  // D2D device contexts are stateful, and hence a unique device context must
  // be used on each call.
  mswr::ComPtr<ID2D1DeviceContext> d2d_context;
  HRESULT hr = directx_context_.d2d_device->CreateDeviceContext(
      D2D1_DEVICE_CONTEXT_OPTIONS_NONE, &d2d_context);
  if (FAILED(hr)) {
    LOG(ERROR) << "Failed to CreateDeviceContext " << std::hex << hr;
    return hr;
  }

  mswr::ComPtr<ID2D1CommandList> print_command_list;
  hr = d2d_context->CreateCommandList(&print_command_list);
  if (FAILED(hr)) {
    LOG(ERROR) << "Failed to CreateCommandList " << std::hex << hr;
    return hr;
  }

  d2d_context->SetTarget(print_command_list.Get());

  d2d_context->BeginDraw();
  d2d_context->DrawGdiMetafile(gdi_metafile);
  hr = d2d_context->EndDraw();
  LOG_IF(ERROR, FAILED(hr)) << "Failed to EndDraw " << std::hex << hr;

  // Make sure to always close the command list.
  HRESULT close_hr = print_command_list->Close();
  LOG_IF(ERROR, FAILED(close_hr)) << "Failed to close command list " << std::hex
                                  << hr;
  if (SUCCEEDED(hr) && SUCCEEDED(close_hr))
    hr = print_control->AddPage(print_command_list.Get(), page_size, NULL);
  if (FAILED(hr))
    return hr;
  else
    return close_hr;
}

size_t PrintDocumentSource::WaitAndGetPageCount() {
  // Properly protect the wait/access to the page count.
  {
    base::AutoLock lock(*parent_lock_);
    if (aborted_)
      return 0;
    DCHECK(pages_.size() == pages_ready_state_.size());
    if (!pages_.empty())
      return pages_.size();
  }
  page_count_ready_.Wait();
  {
    base::AutoLock lock(*parent_lock_);
    if (!aborted_) {
      DCHECK(pages_.size() == pages_ready_state_.size());
      return pages_.size();
    }
  }
  // A page count of 0 means abort.
  return 0;
}

HRESULT PrintDocumentSource::WaitAndGetPage(size_t page_number,
                                            ID2D1GdiMetafile** gdi_metafile) {
  // Properly protect the wait/access to the page data.
  base::AutoLock lock(*parent_lock_);
  // Make sure we weren't canceled before getting here.
  // And the page count should have been received before we get here too.
  if (aborted_)
    return S_FALSE;

  // We shouldn't be asked for a page until we got the page count.
  DCHECK(page_count_ready_.IsSignaled());
  DCHECK(page_number <= pages_ready_state_.size());
  DCHECK(pages_.size() == pages_ready_state_.size());
  while (!aborted_ && pages_[page_number].Get() == NULL)
    pages_ready_state_[page_number]->Wait();

  // Make sure we weren't aborted while we waited unlocked.
  if (aborted_)
    return S_FALSE;
  DCHECK(page_number < pages_.size());

  mswr::ComPtr<ID2D1Factory> factory;
  directx_context_.d2d_device->GetFactory(&factory);

  mswr::ComPtr<ID2D1Factory1> factory1;
  HRESULT hr = factory.As(&factory1);
  if (FAILED(hr)) {
    LOG(ERROR) << "Failed to QI for ID2D1Factory1 " << std::hex << hr;
    return hr;
  }

  ULARGE_INTEGER result;
  LARGE_INTEGER seek_pos;
  seek_pos.QuadPart = 0;
  hr = pages_[page_number]->Seek(seek_pos, STREAM_SEEK_SET, &result);
  if (FAILED(hr)) {
    LOG(ERROR) << "Failed to Seek page stream " << std::hex << hr;
    return hr;
  }

  hr = factory1->CreateGdiMetafile(pages_[page_number].Get(), gdi_metafile);
  if (FAILED(hr)) {
    LOG(ERROR) << "Failed to CreateGdiMetafile " << std::hex << hr;
    return hr;
  }
  return hr;
}

}  // namespace metro_driver