1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef GFX_UPDATEIMAGEHELPER_H
8 #define GFX_UPDATEIMAGEHELPER_H
9 
10 #include "Layers.h"
11 #include "mozilla/layers/ImageClient.h"
12 #include "mozilla/layers/TextureClient.h"
13 #include "mozilla/layers/TextureClientRecycleAllocator.h"
14 #include "mozilla/layers/TextureWrapperImage.h"
15 #include "mozilla/gfx/Types.h"
16 
17 namespace mozilla {
18 namespace layers {
19 
20 class UpdateImageHelper {
21  public:
UpdateImageHelper(ImageContainer * aImageContainer,ImageClient * aImageClient,gfx::IntSize aImageSize,gfx::SurfaceFormat aFormat)22   UpdateImageHelper(ImageContainer* aImageContainer, ImageClient* aImageClient,
23                     gfx::IntSize aImageSize, gfx::SurfaceFormat aFormat)
24       : mImageContainer(aImageContainer),
25         mImageClient(aImageClient),
26         mImageSize(aImageSize),
27         mIsLocked(false) {
28     mTexture = mImageClient->GetTextureClientRecycler()->CreateOrRecycle(
29         aFormat, mImageSize, BackendSelector::Content, TextureFlags::DEFAULT);
30     if (!mTexture) {
31       return;
32     }
33 
34     mIsLocked = mTexture->Lock(OpenMode::OPEN_WRITE_ONLY);
35     if (!mIsLocked) {
36       return;
37     }
38   }
39 
~UpdateImageHelper()40   ~UpdateImageHelper() {
41     if (mIsLocked) {
42       mTexture->Unlock();
43       mIsLocked = false;
44     }
45   }
46 
GetDrawTarget()47   already_AddRefed<gfx::DrawTarget> GetDrawTarget() {
48     RefPtr<gfx::DrawTarget> target;
49     if (mTexture) {
50       target = mTexture->BorrowDrawTarget();
51     }
52     return target.forget();
53   }
54 
UpdateImage()55   bool UpdateImage() {
56     if (!mTexture) {
57       return false;
58     }
59 
60     if (mIsLocked) {
61       mTexture->Unlock();
62       mIsLocked = false;
63     }
64 
65     RefPtr<TextureWrapperImage> image = new TextureWrapperImage(
66         mTexture, gfx::IntRect(gfx::IntPoint(0, 0), mImageSize));
67     mImageContainer->SetCurrentImageInTransaction(image);
68     return mImageClient->UpdateImage(mImageContainer, /* unused */ 0);
69   }
70 
71  private:
72   RefPtr<ImageContainer> mImageContainer;
73   RefPtr<ImageClient> mImageClient;
74   gfx::IntSize mImageSize;
75   RefPtr<TextureClient> mTexture;
76   bool mIsLocked;
77 };
78 
79 }  // namespace layers
80 }  // namespace mozilla
81 
82 #endif  // GFX_UPDATEIMAGEHELPER_H
83