1 /*
2 * Copyright (C) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "mock_producer_surface.h"
17
18 using ::testing::_;
19 using ::testing::Return;
20
21 namespace OHOS {
22 namespace Media {
23 namespace Effect {
24
25 constexpr int32_t WIDTH = 960;
26 constexpr int32_t HEIGHT = 1280;
27 constexpr GraphicPixelFormat PIXEL_FORMAT = GraphicPixelFormat::GRAPHIC_PIXEL_FMT_RGBA_8888;
28
AllocDmaMemory(sptr<SurfaceBuffer> & buffer)29 void MockProducerSurface::AllocDmaMemory(sptr<SurfaceBuffer> &buffer)
30 {
31 BufferRequestConfig requestConfig = {
32 .width = WIDTH,
33 .height = HEIGHT,
34 .strideAlignment = 0x8,
35 .format = PIXEL_FORMAT,
36 .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA | BUFFER_USAGE_MEM_MMZ_CACHE,
37 .timeout = 0,
38 .colorGamut = GraphicColorGamut::GRAPHIC_COLOR_GAMUT_SRGB,
39 .transform = GraphicTransformType::GRAPHIC_ROTATE_NONE,
40 };
41
42 buffer = SurfaceBuffer::Create();
43 buffer->Alloc(requestConfig);
44 buffer->Map();
45 buffer->IncStrongRef(buffer);
46 }
47
ReleaseDmaBuffer(sptr<SurfaceBuffer> & buffer)48 void MockProducerSurface::ReleaseDmaBuffer(sptr<SurfaceBuffer> &buffer)
49 {
50 if (buffer == nullptr) {
51 return;
52 }
53 buffer->DecStrongRef(buffer);
54 buffer = nullptr;
55 }
56
MockProducerSurface(sptr<IBufferProducer> & producer)57 MockProducerSurface::MockProducerSurface(sptr<IBufferProducer>& producer) : ProducerSurface(producer)
58 {
59 AllocDmaMemory(buffer_);
60 ON_CALL(*this, RequestBuffer(_, _, _))
61 .WillByDefault([this](sptr<SurfaceBuffer> &buffer, sptr<SyncFence> &fence, BufferRequestConfig &config) {
62 buffer = buffer_;
63 return GSError::GSERROR_OK;
64 }
65 );
66 ON_CALL(*this, FlushBuffer(_, _, _)).WillByDefault(Return(GSError::GSERROR_OK));
67 }
68
~MockProducerSurface()69 MockProducerSurface::~MockProducerSurface()
70 {
71 ReleaseDmaBuffer(buffer_);
72 }
73
74 } // namespace Effect
75 } // namespace Media
76 } // namespace OHOS